如何将Count(*)传递给@FoundFields变量?请注意,在OECompany表中有2条满足此条件的记录,但@FoundFields的输出为null: - (
--******************
declare @sql nvarchar(4000)
DECLARE @MyTable nvarchar(50);
DECLARE @SecondaryTargetField nvarchar(100)
DECLARE @FoundFields int
set @sql = N'
select
@FoundFields=(count(*))
from
sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
where
c.name LIKE @SecondaryTargetField
and t.name= @MyTable'
SET @MyTable='OECompany'
SET @SecondaryTargetField='%PRODUCTGROUP%'
PRINT @sql
EXEC sp_executesql @sql, N' @FoundFields int OUTPUT, @SecondaryTargetField VARCHAR(100), @MyTable nvarchar(50)',
@FoundFields, @SecondaryTargetField, @MyTable
PRINT @FoundFields
答案 0 :(得分:1)
在OUT
中添加OUTPUT
/ { , [ @param1 = ] 'value1' [OUT|OUTPUT] [ ,...n ] }
个关键字:
EXEC sp_executesql
@sql,
N' @FoundFields int OUTPUT, @SecondaryTargetField VARCHAR(100), @MyTable nvarchar(50)',
@FoundFields OUT, -- <-- OUT|OUTPUT Here
@SecondaryTargetField,
@MyTable
答案 1 :(得分:1)
将OUTPUT关键字添加到要从查询返回的字段中。
EXEC sp_executesql @sql, N' @FoundFields int OUTPUT, @SecondaryTargetField VARCHAR(100), @MyTable nvarchar(50)',
@FoundFields OUTPUT,
@SecondaryTargetField,
@MyTable