在函数内对‘EXECUTE StRING’中对带副作用的或依赖于时间的运算符的使用无效
我想获得第N条数据中的一个字段 为什么我写的函数报错啊???
错误如下:
在函数内对‘EXECUTE StRING’中对带副作用的或依赖于时间的运算符的使用无效
代码如下:
CREATE FUNCTION f_getobjectionveCodeByRESOURCEID(@RsourceID varchar(20),@index int)returns intasbegindeclare @objectiveCode intexecute('select top '+@index+' '+@objectiveCode+'=objectiveCode from sco_e_objectives where ResourceID='+@RsourceID)return @objectiveCodeend
ALTER FUNCTION f_getobjectionveCodeByRESOURCEID(@RsourceID varchar(20),@index int)returns intasbegindeclare @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 @objectiveCodeendgo
[解决办法]
楼上,不是任何时候存储过程都能代替函数的,比方说这个返回值,我想用在select 后,即 select function_name(t.a) from Table t where ... [function_name为标量值函数名],这时存储过程就很无力了。对于这个错我也在找答案,忘哪位仁兄分享分享。。。