RETURN两个语句 - sql函数时得到错误

时间:2014-06-20 06:19:09

标签: sql sql-server function stored-procedures

ALTER FUNCTION [dbo].[UDF_GetExpenseDetails](@EmpID nvarchar(50))    
RETURNS NVARCHAR(MAX) 
AS    
BEGIN    
   DECLARE @ExpAmount nvarchar(MAX)
   DECLARE @ExpName nvarchar(MAX)    

   IF (@EmpID <> '')    
   BEGIN    
      SELECT distinct 
         @ExpAmount = SE.ExpenseAmount, @ExpName = ME.ExpenseName
      FROM 
         tbl_admin_supplierempexpense SE, tbl_master_expense ME
      WHERE 
         SE.EmpID = @EmpID 
         AND SE.ExpenseName = ME.ExpenseID 
         AND SE.Status = 'P'  

   END    

   RETURN @ExpAmount,@ExpName   

END

错误:&#39;,&#39;附近的语法不正确。

3 个答案:

答案 0 :(得分:0)

您无法从FUNCTION返回more than 1个值。如果需要多个值,请创建一个表值函数。

在此处查看http://technet.microsoft.com/en-us/library/ms191165.aspx,包含示例

答案 1 :(得分:0)

如果你试试这个怎么办?

CREATE FUNCTION [owner].[UDF_GetExpenseDetails](@EmpID nvarchar(50)) 
RETURNS TABLE
AS
      RETURN
      (
      SELECT <column1, column2>
      FROM <table>
      WHERE <condition>   
);

答案 2 :(得分:0)

试试这个

     ALTER FUNCTION [dbo].[UDF_GetExpenseDetails](@EmpID nvarchar(50))    
        RETURNS @table( @ExpAmount money,@ExpName varchar(100)) 
        As 
        begin

        IF (@EmpID <> '')    
        BEGIN    
           insert into @ExpAmount 
           SELECT distinct SE.ExpenseAmount,ME.ExpenseName
           from tbl_admin_supplierempexpense SE, tbl_master_expense ME
           where SE.EmpID = @EmpID and SE.ExpenseName=ME.ExpenseID and SE.Status='P'  

        END    

  return

  END