我需要在函数中使用动态 SQL,以便可以执行SELECT * FROM searchAllFields('Pregnant')
. 我知道不可能在函数中使用动态 SQL,但我看到这个人找到了解决方法在函数(SQL Server)中执行动态 sql 时出现错误?
但是当我尝试在 mycode 上应用他的工作时,我得到了A RETURN statement with a return value cannot be used in this context.
它似乎在做同样的事情。为什么我的代码中出现该错误?
ALTER proc [dbo].[searchTerm](@search nvarchar(max))
as
begin
declare @value nvarchar(500),
@SQLString nvarchar(4000),
@stmt nvarchar(max)
select @stmt = isnull(@stmt + ' or ', '') + quotename(name) + ' like @search'
from sys.columns as c
where c.[object_id] = object_id('dbo.table')
select @stmt = 'select @value = id from table where ' + @stmt
exec sp_executesql
@stmt = @stmt,
@params = N'@search nvarchar(max)',
@search = @search,
@value = @value output
return @value
end