0

I have the following tables in sql :

enter image description here

I want to write an function that will take Student_ID as parameter and return the Student_Name and Balance from the above table.

How can I do that ?

2
  • 2
    Which RDBMS is this for? Please add a tag to specify whether you're using mysql, postgresql, sql-server, oracle or db2 - or something else entirely. Commented Apr 2, 2016 at 17:17
  • @osimerpothe I have posted an answer for sql-server...Check whether it is correct or not.. Commented Apr 2, 2016 at 17:18

2 Answers 2

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

you can create procedure from phpmyadmin: How to write a stored procedure in phpMyAdmin?

Sign up to request clarification or add additional context in comments.

Comments

1

Function can be created as:

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;

This is for sql-server. For more information use this link...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.