我已经被困在一个问题上超过两天了,虽然它看起来非常简单(我确定它是)我似乎无法得到它。
我有一个用于报告的查询,我有3个参数。其中2个我输入一个字符串,那很好。虽然是服务器名称的最后一个参数,但我想列出所有可用的或仅列出我输入的那个。
我已经尝试在我的选择中使用CASE,在WHERE子句和许多其他事情中没有运气实现我想要的东西。
以下是我试图展示我所拥有的最后一次尝试。
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 (@p_ServerName = c1.SystemName 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]
所以基本上我希望能够使用@p_ServerName参数查看所有SystemName或特定的SystemName ...任何人都知道如何做到这一点?或者我的尝试出了什么问题?我试过的CASE与WHERE子句中的条件基本相同,只使用了CASE语法。
所有的帮助都非常感谢,因为我已经被困在这几天了...... 干杯!
编辑:这是我尝试过的其他内容,在Management Studio中它运行得很好,但它没有SSRS ...忽略DECLARE,它在管理工作室进行测试......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 --(@p_ServerName = c1.SystemName OR c1.SystemName = c1.SystemName)
c1.SystemName =
CASE
WHEN @p_ServerName = 'all' THEN c1.SystemName
WHEN @p_ServerName != 'all' THEN @p_ServerName
END
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]
我想再次强调一点,在管理工作室中,它工作得很好,只有在ssrs才会遇到问题。
答案 0 :(得分:1)
好吧,让我们试着暴力吧:
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]