加入收藏 | 设为首页 | 会员中心 | 我要投稿 衡阳站长网 (https://www.0734zz.cn/)- 数据集成、设备管理、备份、数据加密、智能搜索!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

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

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

我创建一个函数来执行动态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

(编辑:衡阳站长网)

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

    热点阅读