2016-04-02 23 views
0

sql aşağıdaki tablolar vardır:kullanıcı tanımlı fonksiyonlar

enter image description here

ben parametre olarak student_id alıp yukarıdaki tablodan STUDENT_NAME ve Denge dönecektir bir işlevi yazmak istiyorum .

Bunu nasıl yapabilirim? Bu sql sunucu içindir

CREATE FUNCTION function_name(@Student_ID int) 
RETURNS @name_and_balanceTable TABLE 
(
    -- Columns returned by the function_name 
    Name nvarchar(50), 
    Balance int 
) 
AS 
-- Returns the Name and Balance for particular Student_ID 
BEGIN 
    -- Selecting the name and balance 
    SELECT 
     @Name = st.Student_Name, 
     @Balance = ac.Balance 
    FROM Student st JOIN Account ac ON st.Student_ID = ac.Student_ID 
    WHERE st.Student_ID = @Student_ID; 

    -- Insert these value into table 
    BEGIN 
     INSERT @name_and_balanceTable 
     SELECT @Name, @Balance; 
    END; 
    RETURN; 
END; 

: olarak

+2

? Lütfen "mysql", "postgresql", "sql-server", "oracle" veya "db2" - veya tamamen başka bir şey kullanıp kullanmadığınızı belirtmek için bir etiket ekleyin. –

+0

@osimerpothe sql-server için bir yanıt gönderdim ... Doğru olup olmadığını kontrol edin .. – Shiv

cevap

1

Fonksiyon oluşturulabilir. Daha fazla bilgi kullanım this link için ...

2
CREATE DEFINER=`root`@`localhost` PROCEDURE `myproc`(IN `id` INT) 
NO SQL 
SELECT student.Student_Name, account.Balance 
FROM student INNER JOIN account 
ON student.Student_ID = account.Student_ID 
WHERE student.Student_ID = id 

Eğer phpmyadmin den yordam oluşturabilirsiniz: Bu içindir RDBMS How to write a stored procedure in phpMyAdmin?

İlgili konular