加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 黄冈站长网 (http://www.0713zz.com/)- 数据应用、建站、人体识别、智能机器人、语音技术!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

在函数(SQL Server)中执行动态sql时出错?

发布时间:2021-01-27 12:50:39 所属栏目:MsSql教程 来源:网络整理
导读:我创建一个函数来执行动态SQL并返回一个值.我得到“只有函数和一些扩展存储过程可以在函数内执行.”作为一个错误. 功能: Create Function fn_GetPrePopValue(@paramterValue nvarchar(100))returns int asbegindeclare @value nvarchar(500);Set @SQLStri

我创建一个函数来执行动态SQL并返回一个值.我得到“只有函数和一些扩展存储过程可以在函数内执行.”作为一个错误.

功能:

Create Function fn_GetPrePopValue(@paramterValue nvarchar(100))
returns int as
begin
declare @value nvarchar(500);

Set @SQLString  = 'Select Grant_Nr From Grant_Master where grant_id=' + @paramterValue

exec   sp_executesql
       @query = @SQLString,@value = @value output

return @value   
end

执行:

Select dbo.fn_GetPrePopValue('10002618') from Questions Where QuestionID=114

和:

Select fn_GetPrePopValue('10002618') from Questions Where QuestionID=114

功能是正确调用还是函数不正确?

解决方法

您不能使用函数中的动态SQL,也不能调用
存储过程.
Create proc GetPrePopValue(@paramterValue nvarchar(100))
as
begin
declare @value nvarchar(500),@SQLString nvarchar(4000)

Set @SQLString = 'Select @value = Grant_Nr From Grant_Master where grant_id = @paramterValue'

exec sp_executesql @SQLString,N'@paramterValue nvarchar(100)',@paramterValue,@value = @value output

return @value   
end

(编辑:PHP编程网 - 黄冈站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读