将 Null 值传递给 Dynamic SQL 和 OPENQUERY 的变量
Passing Null values into Variable for Dynamic SQL and OPENQUERY
我有一个查询需要将变量值传递到动态 SQL/openquery 查询中。
declare @week int = 14
declare @id varchar(10) = 10
declare @SQL varchar(MAX), @OQWeek varchar(2) = @Week, @OQID varchar(10) = @ID
SET @sql = N'SELECT * FROM OPENQUERY(SERVER1,''SELECT T.SID , T.AgentNo , T2.Duration
FROM server1.DB1.Table1 T
inner join server1.DB1.Table2 T2 on t2.SID=f.SID
WHERE Week =' + @OQWeek + ' and T2.SID= ISNULL(' + @OQID + ',T2.SID) ' + ''')'
exec( @sql)
如果我在@id 变量中输入一个值,它似乎可以正常运行,但是如果我输入 'NULL'
进入它刚刚返回的变量 Commands completed successfully.
当我也使用 COALESCE() 时也是如此。
如何像在直接 SQL 查询中那样 handle/pass 通过空值? WHERE 子句的正常 SQL 版本通常如下所示
WHERE Week = @OQWeek and T2.SID= ISNULL(@OQID, T2.SID)
如果您的变量之一 @OQWeek
或 @OQID
的值为 NULL
,则整个字符串将解析为 NULL
,如 'Some String' + NULL
= NULL
.
处理动态语句之外的NULL
。因为如果值为 NULL
则不需要子句,所以当它是时不要包含它。我 假设 这仅适用于 @OQWeek
,因为这是你包裹 ISNULL
的变量:
DECLARE @week int = 14,
@id varchar(10) = 10
DECLARE @SQL nvarchar(MAX), --This was the wrong data type
@OQWeek varchar(2)= @Week,
@OQID varchar(10) = @ID,
@CRLF nchar(2) = NCHAR(13) + NCHAR(10);
SET @SQL = N'SELECT * FROM OPENQUERY(SERVER1,N''SELECT T.SID, T.AgentNo , T2.Duration' + @CRLF +
N' FROM server1.DB1.Table1 T' + @CRLF + --do you really have a database called "Server1" on your instance "Server1"?
N' INNER JOIN server1.DB1.Table2 T2 on t2.SID=f.SID' + @CRLF +
N' WHERE Week = ' + @OQWeek + --Careful, syntax like this is open to injection
CASE WHEN @OQID IS NOT NULL THEN @CRLF + N' AND T2.SID = ' + @OQID ELSE '' END + ';'');'
--PRINT @SQL; --Your debugging best friend
EXEC sys.sp_executesql @SQL; --Don't use EXEC(@SQL), you can't parametrise it.
我有一个查询需要将变量值传递到动态 SQL/openquery 查询中。
declare @week int = 14
declare @id varchar(10) = 10
declare @SQL varchar(MAX), @OQWeek varchar(2) = @Week, @OQID varchar(10) = @ID
SET @sql = N'SELECT * FROM OPENQUERY(SERVER1,''SELECT T.SID , T.AgentNo , T2.Duration
FROM server1.DB1.Table1 T
inner join server1.DB1.Table2 T2 on t2.SID=f.SID
WHERE Week =' + @OQWeek + ' and T2.SID= ISNULL(' + @OQID + ',T2.SID) ' + ''')'
exec( @sql)
如果我在@id 变量中输入一个值,它似乎可以正常运行,但是如果我输入 'NULL'
进入它刚刚返回的变量 Commands completed successfully.
当我也使用 COALESCE() 时也是如此。
如何像在直接 SQL 查询中那样 handle/pass 通过空值? WHERE 子句的正常 SQL 版本通常如下所示
WHERE Week = @OQWeek and T2.SID= ISNULL(@OQID, T2.SID)
如果您的变量之一 @OQWeek
或 @OQID
的值为 NULL
,则整个字符串将解析为 NULL
,如 'Some String' + NULL
= NULL
.
处理动态语句之外的NULL
。因为如果值为 NULL
则不需要子句,所以当它是时不要包含它。我 假设 这仅适用于 @OQWeek
,因为这是你包裹 ISNULL
的变量:
DECLARE @week int = 14,
@id varchar(10) = 10
DECLARE @SQL nvarchar(MAX), --This was the wrong data type
@OQWeek varchar(2)= @Week,
@OQID varchar(10) = @ID,
@CRLF nchar(2) = NCHAR(13) + NCHAR(10);
SET @SQL = N'SELECT * FROM OPENQUERY(SERVER1,N''SELECT T.SID, T.AgentNo , T2.Duration' + @CRLF +
N' FROM server1.DB1.Table1 T' + @CRLF + --do you really have a database called "Server1" on your instance "Server1"?
N' INNER JOIN server1.DB1.Table2 T2 on t2.SID=f.SID' + @CRLF +
N' WHERE Week = ' + @OQWeek + --Careful, syntax like this is open to injection
CASE WHEN @OQID IS NOT NULL THEN @CRLF + N' AND T2.SID = ' + @OQID ELSE '' END + ';'');'
--PRINT @SQL; --Your debugging best friend
EXEC sys.sp_executesql @SQL; --Don't use EXEC(@SQL), you can't parametrise it.