EXEC sp_executesql不起作用或不返回数据

时间:2017-11-30 20:06:45

标签: sql-server exec sp-executesql

我有来自触发器的代码部分。当我测试我的触发器时,我的exec不会返回任何数据。

我将我的exec值声明为:

declare
    @sql nvarchar(MAX), 
    @SQLString varchar(MAX)

这是选择:

set @sql =  N'select ' 
        set @sql = @sql +   '@SQLString = convert(varchar(1000),d.' + @fieldname + ')' +' + convert(varchar(1000),i.' + @fieldname + ')'
set @sql = @sql +   ' from #ins i full outer join #del d'
set @sql = @sql +   @PKCols --
set @sql = @sql +   ' where i.' + @fieldname + ' <> d.' + @fieldname 
set @sql = @sql +   ' or (i.' + @fieldname + ' is null and  d.' + @fieldname + ' is not null)' 
set @sql = @sql +   ' or (i.' + @fieldname + ' is not null and  d.' + @fieldname + ' is null)' 

(所有变量都可以正常工作):就像我打印@sql一样,它会喜欢这个 - &gt;

select @SQLString = convert(varchar(1000), d.ActionID) + convert(varchar(1000), i.ActionID) 
from #ins i 
full outer join #del d on i.ActionID = d.ActionID 
where i.ActionID <> d.ActionID 
   or (i.ActionID is null and d.ActionID is not null) 
   or (i.ActionID is not null and d.ActionID is null)

这是执行:

EXEC sp_executesql @sql, 
        N'@SQLString varchar(MAX) OUTPUT', 
        @SQLString = @SQLString OUTPUT;

但是打印没有显示任何内容:

print '@SQLString:' + @SQLString;

2 个答案:

答案 0 :(得分:0)

当使用'+'运算符将Null与字符串连接时,您将获得Null。

请改用CONCAT功能。它将参数转换为字符串,并将Null视为空字符串。

答案 1 :(得分:0)

调查你的select语句,从一些测试数据上运行它,没有返回来自#ins&amp;的ActionID的情况。 #del非空,一个对我来说总是空的。

这会导致您添加一个非null和一个空字符串,即

convert(varchar(1000), d.ActionID) + convert(varchar(1000), i.ActionID)

然后返回null,结果@SQLString为null。

我把一些测试代码放在一起:

CREATE TABLE #ins 
(
    ActionID INT
)

CREATE TABLE #del
(
    ActionID INT
)

INSERT INTO #ins
VALUES (1),(3),(5),(7),(9)

--Data sets mostly disjoint, have 1 overlapping value to see what happens.
INSERT INTO #del
VALUES (2),(4),(6),(8),(9),(10)

--Visual demo of return values.
SELECT * 
FROM #ins i 
     FULL OUTER JOIN #del d ON i.ActionID = d.ActionID 
WHERE i.ActionID != d.ActionID 
      OR (i.ActionID IS NULL AND d.ActionID IS NOT NULL) 
      OR (i.ActionID IS NOT NULL AND d.ActionID IS NULL)

--Original SQL to demonstrate NULL return value.
DECLARE @sql NVARCHAR(MAX) = 'SELECT @SQLString = CONVERT(NVARCHAR(1000), d.ActionID) + CONVERT(NVARCHAR(1000), i.ActionID) FROM #ins i FULL OUTER JOIN #del d ON i.ActionID = d.ActionID WHERE i.ActionID != d.ActionID OR (i.ActionID IS NULL AND d.ActionID IS NOT NULL) OR (i.ActionID IS NOT NULL AND d.ActionID IS NULL)'
       ,@SQLString NVARCHAR(MAX)

EXEC sp_executesql @sql, N'@SQLString NVARCHAR(MAX) OUTPUT', @SQLString = @SQLString OUTPUT

SELECT @SQLString

--Modified original vars/sql and wrapped in ISNULL to demonstrate non-null return value.
DECLARE @sql2 NVARCHAR(MAX) = 'SELECT @SQLString = CONVERT(NVARCHAR(1000), ISNULL(d.ActionID,0)) + ''-'' + CONVERT(NVARCHAR(1000), ISNULL(i.ActionID,0)) FROM #ins i FULL OUTER JOIN #del d ON i.ActionID = d.ActionID WHERE i.ActionID != d.ActionID OR (i.ActionID IS NULL AND d.ActionID IS NOT NULL) OR (i.ActionID IS NOT NULL AND d.ActionID IS NULL)'
       ,@SQLString2 NVARCHAR(MAX)

EXEC sp_executesql @sql2, N'@SQLString NVARCHAR(MAX) OUTPUT', @SQLString = @SQLString OUTPUT

SELECT @SQLString