0

當我在SQL Server上運行下面的查詢時,它對單值或多值工作正常。但是當我嘗試在SSRS數據集上執行相同的操作時,它將爲多個值引發錯誤。在上下文中指定的非布爾類型的表達式,其中條件接近','

ERROR: SSRS: An expression of non boolean type specified in a context where a condition is expected near ','. Incorrect syntax near the keyword 'ELSE'.

它工作正常,單值。不知道是什麼問題,或者有什麼其他的方式可以寫這個查詢。

DECLARE @UserTeam VARCHAR(max) = 'Team1,Team2,Team3,Team4,Team5,Team6,''' 

-- Values can inculdes null. if user select blank and Team1,Team2 we need to pull data which is null, Team1, Team2 
IF Object_id('tempdb.dbo.#Team', 'U') IS NOT NULL 
    DROP TABLE #team; 

SELECT item AS Team 
INTO #team 
FROM dbo.FNSPLIT(@UserTeam, ',') 

IF @UserTeam = '' 
    BEGIN 
     SELECT U.username, 
      T.teamcode 
     FROM [User].vwuser u 
      LEFT JOIN[User].userteam UT 
        ON U.userid = UT.userid 
      LEFT JOIN [User].team AS T 
        ON T.teamid = Ut.teamid 
     WHERE T.teamcode IS NULL 
     ORDER BY U.username 
    END 
ELSE 
    BEGIN 
     SELECT U.username, 
      T.teamcode 
     FROM [User].vwuser u 
      LEFT JOIN [User].userteam UT 
        ON U.userid = UT.userid 
      LEFT JOIN [User].team AS T 
        ON T.teamid = Ut.teamid 
      JOIN #team tm 
       ON T.teamcode = tm.team 
     WHERE (U.username NOT LIKE '%System%') 
     ORDER BY U.username 
    END 

回答

0

我想通了什麼問題。這是因爲UserTeam參數不接受多個選擇。解決我添加下面的表達式。

=JOIN(Parameters!UserTeam.Value,",") 

數據集屬性 - >參數 - > @UserTeam - >添加表達式。基本上它會用逗號分隔值和函數分隔dbo.fnSplit(@UserTeam,',')會轉換成表格。

而且我已經改變以更好的方式查詢,而不必IF條件

SELECT U.userName, T.teamCode 
FROM [User].vwUser u 
    left join [User].UserTeam UT ON U.userId = UT.userId 
    left join [User].Team AS T ON T.teamId = Ut.teamId 
WHERE   
    U.userName NOT LIKE '%System%' 
    AND ISNULL(T.teamCode,'') IN (SELECT ISNULL(item,'') FROM dbo.fnSplit(@UserTeam,',')) 
ORDER BY U.userName 
0

可以執行下句,測試功能fnSplit:

DECLARE @UserTeam VARCHAR(MAX)= 'TEAM1,的Team2,Team3,Team4,Team5,Team6,' ''

AS隊SELECT項目FROM dbo.fnSplit(@UserTeam, '')

如果上述功能是否正常工作,你會看到七行:

  • TEAM1
  • 的Team2
  • Team3
  • Team4
  • Team5
  • Team6
  • 「(單引號)
+0

是的,我給'來表示爲空來解釋。因爲如果我在SSRS參數中選擇空白字段,我不確定它傳遞的值是多少 – crony

相關問題