日期:2014-05-18  浏览次数:21710 次

在函数内对‘EXECUTE StRING’中对带副作用的或依赖于时间的运算符的使用无效
我想获得第N条数据中的一个字段 为什么我写的函数报错啊???

错误如下:
在函数内对‘EXECUTE StRING’中对带副作用的或依赖于时间的运算符的使用无效

代码如下:
SQL code

CREATE FUNCTION f_getobjectionveCodeByRESOURCEID
(@RsourceID varchar(20),@index int)
returns int
as
begin
declare @objectiveCode int
execute('select top '+@index+' '+@objectiveCode+'=objectiveCode from sco_e_objectives where ResourceID='+@RsourceID)

return @objectiveCode
end



急急……

------解决方案--------------------
花了点时间,这代码真不好写:
SQL code
ALTER FUNCTION f_getobjectionveCodeByRESOURCEID
(@RsourceID varchar(20),@index int)
returns int
as
begin
declare @objectiveCode int, @SQL NVARCHAR(200)
set @SQL='select top 1 @objectiveCode=objectiveCode from (select top '+@index+' objectiveCode from sco_e_objectives where ResourceID='+@RsourceID +')a';
EXEC SP_EXECUTESQL @SQL,N'@objectiveCode int', 
        @objectiveCode OUT;
return @objectiveCode
end
go

------解决方案--------------------
楼上,不是任何时候存储过程都能代替函数的,比方说这个返回值,我想用在select 后,即 select function_name(t.a) from Table t where ... [function_name为标量值函数名],这时存储过程就很无力了。对于这个错我也在找答案,忘哪位仁兄分享分享。。。