Sql server 2012 Sql Server 2012如何获取函数上的列表

Sql server 2012 Sql Server 2012如何获取函数上的列表,sql-server-2012,Sql Server 2012,功能详情: alter FUNCTION siralama_puan (@suggestion_id int) RETURNS int AS Begin Declare @comment_count int,@like_count int,@favorite_count int,@date_point int,@suggestion_point int,@suggestion_date datetime,@fark int set @comment_count=(selec

功能详情:

alter FUNCTION siralama_puan (@suggestion_id int)
RETURNS int
AS
Begin
     Declare @comment_count int,@like_count int,@favorite_count int,@date_point int,@suggestion_point int,@suggestion_date datetime,@fark int

     set @comment_count=(select [Suggestion].CommentCount from [Suggestion] where  [Suggestion].Id= @suggestion_id)
     set @like_count=(select [Suggestion].LikeCount from [Suggestion] where [Suggestion].Id=@suggestion_id)
     set @favorite_count=(select [Suggestion].FavoriteCount from [Suggestion] where [Suggestion].Id=@suggestion_id)
     set @suggestion_date=(select [Suggestion].Crtm from [Suggestion] where [Suggestion].Id=@suggestion_id)
     set @fark =(select DATEDIFF(day,@suggestion_date,GETDATE()))

     if @fark<6  
     set @date_point=30
    else if @fark<10 and @fark>=6
     set @date_point=20
    else
    set @date_point=10

     set @suggestion_point=(@comment_count*2)+(@like_count)+(@favorite_count*3)+@date_point
     RETURN @suggestion_point
End

但它不起作用。错误:过程或函数dbo.siralama\u puan指定的参数太多。多个参数不起作用。

无法定义一个INT类型的参数并在列表中传递。但有几种方法:

类型 使用
Create type
创建您自己的类型。这允许您传入一个非常类似于(只读)表的列表

XML 让您的参数为XML类型,并传入如下内容

<root>
   <prm value="122280"/>
   <prm value="1"/>
</root>
SELECT Prm.value('@value','int') AS PrmValue
FROM @prm.nodes('/root/prm') AS One(Prm)
CSV
类型为
VARCHAR(MAX)
的参数和逗号分隔的列表,如“122280,1”。找到一种方法来拆分此

不可能定义一个INT类型的参数并在列表中传递。但有几种方法:

类型 使用
Create type
创建您自己的类型。这允许您传入一个非常类似于(只读)表的列表

XML 让您的参数为XML类型,并传入如下内容

<root>
   <prm value="122280"/>
   <prm value="1"/>
</root>
SELECT Prm.value('@value','int') AS PrmValue
FROM @prm.nodes('/root/prm') AS One(Prm)
CSV
类型为
VARCHAR(MAX)
的参数和逗号分隔的列表,如“122280,1”。找到一种方法来拆分这个

当然,它不起作用,因为函数只接收一个参数。你的问题是什么?我想发送多个id。结果会是什么?你需要修改函数参数以获取逗号分隔的id。然后将逗号分隔的ID转换为temp table,并通过temp table行循环,将r insert转换为表格变量并返回表格。@ashy,逗号分隔的列表只是一种方法,至少对我来说不是最好的方法。。。最好使用
createtype
或XML(见我的答案)。当然,它不起作用,因为函数只接收一个参数。你的问题是什么?我想发送多个id。结果会是什么?你需要修改函数参数以获取逗号分隔的id。然后将逗号分隔的ID转换为temp table,并通过temp table行循环,将r insert转换为表格变量并返回表格。@ashy,逗号分隔的列表只是一种方法,至少对我来说不是最好的方法。。。最好使用
createtype
或XML(参见我的答案)。