Parameter functioning in SSMS but not SSRS

放肆的年华 提交于 2020-01-17 00:53:41

问题


Assume I have the following query (simplified version of what im really using)...

DECLARE @p_ServerName nvarchar(10)
SELECT  SystemName FROM Table_x
WHERE   SystemName = @p_ServerName OR @p_ServerName = 'all'

When I set the parameter to a specific name ex: DB1 the rows are displayed in SSRS but when I input 'all' as the parameter, nothing is displayed. Yet when I do the same in SSMS it works just fine.

Does anyone know what can be the problem here? Thanks.

EDIT

Here is the first query I tried (ignore the declares, it was just used for ssms testing in which it worked just fine):

DECLARE    @p_ServerName nvarchar(10) = 'all'
DECLARE    @p_Env        nvarchar(10) = 'all'
DECLARE    @p_EnvCat     nvarchar(10) = 'all'

SELECT DISTINCT    
           c1.BlockSize, c1.BootVolume, c1.Compressed, c1.SystemName, c1.Label, c1.Caption, c1.PageFilePresent,
           [dbo].[CCS_DIGITAL_STORAGE_CONVERTER]('B', 'GB', c1.Capacity) AS Capacity,
           [dbo].[CCS_DIGITAL_STORAGE_CONVERTER]('B', 'GB', c1.FreeSpace) AS [Free Space], 
           [dbo].[CCS_DIGITAL_STORAGE_CONVERTER]('B', 'GB', c1.Capacity - c1.FreeSpace) AS [Used Space],
           100 * c1.FreeSpace / c1.Capacity AS [Free Space %],
           [CLE_ENV_SHORT], [CLE_ENV_CAT_SHORT]

FROM       CCS_Win32_Volume c1

JOIN       [dbo].[CCS_V_SERVER_INSTANCE_DETAILS] c2 on c1.SystemName = c2.CSL_SERVER_NAME

WHERE     (c1.SystemName = @p_ServerName OR c1.SystemName = c1.SystemName)
     AND   (c2.[CLE_ENV_SHORT] = @p_Env OR @p_Env = 'all')
     AND   (c2.[CLE_ENV_CAT_SHORT] = @p_EnvCat OR @p_EnvCat = 'all')

ORDER BY   c2.[CLE_ENV_CAT_SHORT], c2.[CLE_ENV_SHORT]

Here is the other query i tried (again ignore the declares):

    DECLARE    @p_ServerName nvarchar(10) = 'all'
    DECLARE    @p_Env        nvarchar(10) = 'all'
    DECLARE    @p_EnvCat     nvarchar(10) = 'all'

    SELECT DISTINCT   
           c1.SystemName, c1.BlockSize, c1.BootVolume, c1.Compressed, c1.Label, c1.Caption, c1.PageFilePresent,
           [dbo].[CCS_DIGITAL_STORAGE_CONVERTER]('B', 'GB', c1.Capacity) AS Capacity,
           [dbo].[CCS_DIGITAL_STORAGE_CONVERTER]('B', 'GB', c1.FreeSpace) AS [Free Space], 
           [dbo].[CCS_DIGITAL_STORAGE_CONVERTER]('B', 'GB', c1.Capacity - c1.FreeSpace) AS [Used Space],
           100 * c1.FreeSpace / c1.Capacity AS [Free Space %],
           [CLE_ENV_SHORT], [CLE_ENV_CAT_SHORT]
    FROM       CCS_Win32_Volume c1
    JOIN       [dbo].[CCS_V_SERVER_INSTANCE_DETAILS] c2 on c1.SystemName = c2.CSL_SERVER_NAME
    WHERE      c1.SystemName = @p_ServerName
         AND   (c2.[CLE_ENV_SHORT] = @p_Env OR @p_Env = 'all')
         AND   (c2.[CLE_ENV_CAT_SHORT] = @p_EnvCat OR @p_EnvCat = 'all')

    UNION

    SELECT DISTINCT    
           c1.SystemName, c1.BlockSize, c1.BootVolume, c1.Compressed, c1.Label, c1.Caption, c1.PageFilePresent,
           [dbo].[CCS_DIGITAL_STORAGE_CONVERTER]('B', 'GB', c1.Capacity) AS Capacity,
           [dbo].[CCS_DIGITAL_STORAGE_CONVERTER]('B', 'GB', c1.FreeSpace) AS [Free Space], 
           [dbo].[CCS_DIGITAL_STORAGE_CONVERTER]('B', 'GB', c1.Capacity - c1.FreeSpace) AS [Used Space],
           100 * c1.FreeSpace / c1.Capacity AS [Free Space %],
           [CLE_ENV_SHORT], [CLE_ENV_CAT_SHORT]
    FROM       CCS_Win32_Volume c1
    JOIN       [dbo].[CCS_V_SERVER_INSTANCE_DETAILS] c2 on c1.SystemName = c2.CSL_SERVER_NAME
    WHERE      @p_ServerName = 'all'
         AND   (c2.[CLE_ENV_SHORT] = @p_Env OR @p_Env = 'all')
         AND   (c2.[CLE_ENV_CAT_SHORT] = @p_EnvCat OR @p_EnvCat = 'all')

   ORDER BY   c2.[CLE_ENV_CAT_SHORT], c2.[CLE_ENV_SHORT], c1.SystemName 

Since multiple variations of queries work fine in SSMS I am assuming the issue is how SSRS handles 'all'. Because when I enter any server name it works fine. Only 'all' has a problem. So I guess my question is, how do you set up parameters to do this?


回答1:


It isn't very clear from your question which parameter you're having trouble implementing the "ALL" filtering on. But from looking at your two queries it looks like it is the @p_ServerName parameter. In your first query the other two seem to have had the 'ALL' filtering done properly.

So the problem must be here:

(c1.SystemName = @p_ServerName OR c1.SystemName = c1.SystemName)

First, what are you trying to achieve with the second part? c1.SystemName=c1.SystemName will always be true (unless c1.SystemName is NULL and if that is your goal then use c1.SystemName IS NOT NULL

I think this should look more like:

(c1.SystemName=@p_ServerName OR @p_ServerName='all')

Which is how you've implemented the other two parameters, so perhaps I'm missing something.

Also, I'm not sure what you mean when you say that the UNIONed query solution must be how SSRS is doing things. SSRS doesn't rewrite your queries based on the parameters, it just sticks that values in those variables for you to use in your query. If your parameter @p_ServerName is a string then it is just a VARCHAR variable with the characters "all" in it.



来源:https://stackoverflow.com/questions/23791584/parameter-functioning-in-ssms-but-not-ssrs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!