我创建了一个函数来执行动态 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
关于sql - 在函数 (SQL Server) 中执行动态 sql 时出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15180561/