【问题标题】:how to return multiple values from User defined function in SQL如何从 SQL 中的用户定义函数返回多个值
【发布时间】:2015-10-11 18:25:54
【问题描述】:

需要有关我创建的 SQL 用户定义函数的帮助。 我的函数应该根据我给出的项目编号返回项目的类型。 当我执行该函数时,我得到一个错误

"子查询返回超过 1 个值。当 子查询遵循 =、!=、、>= 或当子查询用作 一个表达式。”

我想我应该将此函数的返回类型更改为表格。但是我不知道该怎么做。 这是我的功能:

create function [dbo].[fx_calculate_type](@item varchar)
returns varchar(10)
AS 
begin
DECLARE @type VARCHAR(10)
       ,@typeCount int
       ,@MaxYear int 
       ,@redoitem varchar(18)

set @type = ''
set @typeCount = (Select count(m.year) 
                  from mr m
                  where m.item_no = 'RR301') --@item  
set @MaxYear =   (Select Max(m.year) 
                  from mr m
                  where m.item_no = 'RR301') --@item  
set @redoitem =  (select redoitem 
                  from  mr m
                  where m.item_no = 'RR301') --@item   

       if (@redoitem is null or @redoitem= '')

      BEGIN 
      While (@typeCount>=1)
         Begin
         Continue 
            If @typeCount = 1
                Begin 
                set @type = 'N'
                   --return (@type )
                End
            Else 
                Begin
                set @type = @typeCount+ 'C'
                set @MaxYear =@MaxYear -1 --2014
                set @typeCount = @typeCount -1  --4
                  -- return (@type) 
                END
          END
       END 

       Else
          BEGIN 
      While (@typeCount>=1)
         Begin
         Continue 
            If @typeCount = 1
                Begin 
                set @type = 'N'
                   --return (@type)  
                End
            Else 
                Begin
                set @type = @typeCount+ 'R'
                set @MaxYear =@MaxYear -1 --2014
                set @typeCount = @typeCount -1  --4
                   --return (@type)  
                END
          END
       END
     return (@type) 
      END 

我怎样才能让它工作?

【问题讨论】:

  • 您的代码对于您给出的描述来说似乎太复杂了。但是带有子查询的第三个set 会导致您描述的错误。
  • 您的循环看起来需要一个表值函数,这是一个标量函数,请参阅msdn.microsoft.com/en-us/library/ms186755.aspx
  • 您需要一个多语句表值函数并从该函数返回一个表或使用具有多个 OUTPUT 参数的存储过程(首选)。此外,您的函数采用 varchar 类型的参数,未定义任何长度,传递给此变量的任何内容都将被截断为默认长度 1。您需要明确定义此参数的长度。

标签: sql sql-server function user-defined-functions


【解决方案1】:

这对我有用!谢谢

创建过程 [dbo].[类型] @item varchar(10) 作为 开始 声明 @type VARCHAR(10) ,@typeCount 整数
,@MaxYear 整数 ,@redoitem varchar(18)

设置@type = '' 设置@typeCount =(选择计数(DISTINCT m.year) 来自米先生 其中 m.item_no = @item) --@item

set @MaxYear = (选择不同的 Max(m.year) 来自米先生 其中 m.item_no = @item)

set @redoitem = (选择前 1 个重做项 来自米先生 其中 m.item_no = @item)

IF(@redoitem 为空或@redoitem='')开始
WHILE (@typeCount>=1) 开始

    IF @typeCount = 1 
        BEGIN 
    SET @type = 'N'     
    UPDATE mr  SET type =@type WHERE item_no = @item AND year = @MaxYear    
    END
    ELSE        
        BEGIN

        set @type = CONVERT(VARCHAR(10),@typeCount)+ 'C'    
        UPDATE mr  SET type =@type WHERE item_no = @item AND year = @MaxYear
        SET @MaxYear =@MaxYear -1

    END

        SET @typeCount = @typeCount -1  
    CONTINUE
END

结束

其他开始 While (@typeCount>=1) 开始

如果@typeCount = 1 开始 设置@type = 'N' UPDATE mr SET type =@type WHERE item_no = @item AND year = @MaxYear 结尾 别的 开始 设置@type = CONVERT(VARCHAR(10),@typeCount)+ 'R' UPDATE mr SET type =@type WHERE item_no = @item AND year = @MaxYear 设置@MaxYear =@MaxYear -1 --2014 设置@typeCount = @typeCount -1 --4

结束

    CONTINUE
  END

结束 结束

【讨论】:

    猜你喜欢
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    • 2016-02-29
    • 1970-01-01
    • 2014-06-20
    • 2010-09-21
    相关资源
    最近更新 更多