使用print @sql调试动态SQL吗?

时间:2018-06-19 19:49:49

标签: sql-server reporting-services dynamic-sql

我无法在这里发现我做错了什么。我希望能够使用print命令进行调试,但是我无法使其正常工作,请在此处指出我做错了什么

= " SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;"
+ " DECLARE @sql NVARCHAR(MAX)"
+ " SELECT @sql = COALESCE(@sql + N' UNION ', N'') + N'hr.client ,RTRIM(LTRIM(hc.facility)) AS face ,h.gode ,h.rode    ,hr.*"
+ " FROM B1' + CONVERT(NVARCHAR(MAX), RIGHT(CONCAT('00000',dbname),5)) + '.dbo.herpa h "
+ " JOIN B1' + CONVERT(NVARCHAR(MAX), RIGHT(CONCAT('00000',dbname),5)) + '.dbo.herpaderpa hr ON hr.[counter] = h.rink "
+ " JOIN B1' + CONVERT(NVARCHAR(MAX), RIGHT(CONCAT('00000',dbname),5)) + '.dbo.herpacerpa hc ON hc.[counter] = hr.cink " 
+ " WHERE 1=1  "
+ IIf(IsNothing(Parameters!StartDate.Value),"", " AND (hr.r_date > DATEADD(DAY,-1,''"
+ Parameters!StartDate.Value
+ "'')) ")
+ IIf(IsNothing(Parameters!EndDate.Value),"", " AND (hr.r_date < DATEADD(DAY,1,''"
+ Parameters!EndDate.Value
+ "'')) ")


+ " AND hr.NPI IN (''" + Join(Parameters!NPI.Value,"'',''") + "'') "
+ IIf(Array.IndexOf(Parameters!Cat.Value, "ANY") > -1, "", " AND hr.cat IN (''" + Join(Parameters!Cat.Value,"'',''") + "'') ")
+ IIf(Array.IndexOf(Parameters!Gode.Value, "ANY") > -1, "", " AND h.gode IN (''" + Join(Parameters!Gode.Value,"'',''") + "'') ")
+ IIf(Array.IndexOf(Parameters!Rode.Value, "ANY") > -1, "", " AND h.rode IN (''" + Join(Parameters!Rode.Value,"'',''") + "'') ")

+ "' "
+ " FROM (VALUES "
+ Parameters!SC.Value 
+ ") t (dbname) "
+ " exec sp_executesql @sql "

我已将整个内容包装在@SQL中,然后尝试打印@sql 然而,那什么也没返回。请让我知道我在做什么错

2 个答案:

答案 0 :(得分:0)

请参阅本节:

assets/

的第一行以 + " WHERE 1=1 and " + IIf(IsNothing(Parameters!StartDate.Value),"", " AND (hr.r_date > DATEADD(DAY,-1,''" 结尾。

在下一行,如果值不是Nothing,则添加一个以 and 开始的

通往AND

答案 1 :(得分:0)

我是个白痴,等于。

我仍然对如何将其包装在Print语句中有零的想法,但是我能够发现问题。

+ " SELECT @sql = COALESCE(@sql + N' UNION ', N'') + N'hr.client ,RTRIM(LTRIM(hc.facility)) AS face ,h.gode ,h.rode    ,hr.*"

应该是

+ " SELECT @sql = COALESCE(@sql + N' UNION ', N'') + N'select hr.client ,RTRIM(LTRIM(hc.facility)) AS face ,h.gode ,h.rode    ,hr.*"

无论如何,我忘记了最初的选择。显然我是个白痴