【问题标题】:user defined functions returning multiple values in sql用户定义的函数在sql中返回多个值
【发布时间】:2016-07-22 09:45:23
【问题描述】:

我在 sql 中有以下表格:

我想编写一个函数,它将 Student_ID 作为参数并从上表中返回 Student_Name 和 Balance。

我该怎么做?

【问题讨论】:

  • 这适用于哪个 RDBMS?请添加标签以指定您使用的是mysqlpostgresqlsql-serveroracle 还是db2 - 或其他完全不同的东西。
  • @osimerpothe 我已经发布了 sql-server 的答案...检查它是否正确..

标签: sql


【解决方案1】:
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

您可以从 phpmyadmin 创建程序: How to write a stored procedure in phpMyAdmin?

【讨论】:

    【解决方案2】:

    函数可以创建为:

    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;
    

    这是用于 sql-server 的。 欲了解更多信息,请使用this link...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-29
      • 2018-08-29
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      相关资源
      最近更新 更多