在函数(SQL Server)中执行动态sql时出错?
发布时间:2021-01-25 21:15:57 所属栏目: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 (编辑:92站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |