SQL Conditional Where语句?

时间:2013-03-19 23:06:23

标签: sql conditional where statements

我在一家医院集团工作,我们有多个站点服务器,组织到大约6个地区。我正在SCCM中编写一个sql查询,并希望用站点解析区域,这证明是一个挑战...我想根据我查询的区域做一个有选择性的where语句,但我不确定如果可以的话。我知道以下代码已被破坏...特别是where行附近的case语句。在这种情况下是否有类似的东西可供使用?

DECLARE @test AS varchar(4) = 'WBR';
SELECT 
    DISTINCT MEMB.Netbios_Name0 AS [Computer Name],
    v_GS_COMPUTER_SYSTEM.Manufacturer0 AS [Computer Manufacturer], 
    v_GS_COMPUTER_SYSTEM.Model0 AS [Computer Model], 
    v_GS_INSTALLED_SOFTWARE_CATEGORIZED.ProductName0 AS [Product Name], 
    v_GS_INSTALLED_SOFTWARE_CATEGORIZED.Publisher0 AS [Publisher],
    v_GS_INSTALLED_SOFTWARE_CATEGORIZED.SoftwareID AS [SoftwareID], 
    MEMB.SiteCode AS [Site],
    CASE MEMB.SiteCode
        When 'AB1' Then 'EBR' 
        When 'AM1' Then 'SSR'
        When 'CMB' Then 'PCR'
        When 'CO1' Then 'WBR'
        When 'CP1' Then 'WBR'
        When 'CUC' Then 'PCR'
        When 'DM1' Then 'EBR'
        When 'EM1' Then 'EBR'
        When 'LS1' Then 'WBR'
        When 'MM1' Then 'CVR'
        When 'MM2' Then 'CVR'
        When 'MP1' Then 'PCR'
        When 'MP2' Then 'PCR'
        When 'NM1' Then 'SSR'
        When 'NV1' Then 'WBR'
        When 'PA1' Then 'PCR'
        When 'PAF' Then 'PCR'
        When 'PDU' Then 'PCR'
        When 'PLA' Then 'PCR'
        When 'PMF' Then 'PCR'
        When 'PMO' Then 'PCR'
        When 'PMP' Then 'PCR'
        When 'POR' Then 'PCR'
        When 'PPV' Then 'PCR'
        When 'PRS' Then 'PCR'
        When 'PSA' Then 'PCR'
        When 'SC1' Then 'CCS'
        When 'SF1' Then 'SSR'
        When 'SG1' Then 'SSR'
        When 'SH1' Then 'PCR'
        When 'SM1' Then 'SSR'
        When 'SR1' Then 'WBR'
        When 'SS1' Then 'CCS'
        When 'SZ1' Then 'PCR'
        When 'SZ2' Then 'PCR'
        When 'VN1' Then 'SCH'
        else 'Unknown'
    end 'Region',
    v_GS_COMPUTER_SYSTEM.UserName0 AS [Last User]

    FROM v_GS_INSTALLED_SOFTWARE_CATEGORIZED 
    INNER JOIN 
    ( 
        select 
            distinct v_R_System_Valid.ResourceID, 
            v_FullCollectionMembership.SiteCode,
            v_R_System_Valid.Netbios_Name0 

            from v_R_System_Valid 
            INNER JOIN v_FullCollectionMembership ON    v_FullCollectionMembership.ResourceID = v_R_System_Valid.ResourceID 

            case @test
                when 'SCAH' Then (where v_FullCollectionMembership.SiteCode='VN1')
                when 'CCS' Then (where v_FullCollectionMembership.SiteCode='SS1' OR v_FullCollectionMembership.SiteCode='SC1' )
                when 'CVR' Then (where v_FullCollectionMembership.SiteCode='MM1' OR v_FullCollectionMembership.SiteCode='MM2')
                when 'EBR' Then (where v_FullCollectionMembership.SiteCode='AB1' OR v_FullCollectionMembership.SiteCode='DM1' OR v_FullCollectionMembership.SiteCode='EM1')
                when 'PCR' Then (where v_FullCollectionMembership.SiteCode='CMB' OR v_FullCollectionMembership.SiteCode='CUC' OR v_FullCollectionMembership.SiteCode='MP1' OR v_FullCollectionMembership.SiteCode='MP2' OR v_FullCollectionMembership.SiteCode='PA1' OR v_FullCollectionMembership.SiteCode='PAF' OR v_FullCollectionMembership.SiteCode='PDU' OR v_FullCollectionMembership.SiteCode='PLA' OR v_FullCollectionMembership.SiteCode='PMF' OR v_FullCollectionMembership.SiteCode='PMO' OR v_FullCollectionMembership.SiteCode='PMP' OR v_FullCollectionMembership.SiteCode='POR' OR v_FullCollectionMembership.SiteCode='PPV' OR v_FullCollectionMembership.SiteCode='PRS' OR v_FullCollectionMembership.SiteCode='PSA' OR v_FullCollectionMembership.SiteCode='SH1' OR v_FullCollectionMembership.SiteCode='SZ1' OR v_FullCollectionMembership.SiteCode='SZ2')
                when 'SSR' Then (where v_FullCollectionMembership.SiteCode='AM1' OR v_FullCollectionMembership.SiteCode='NM1' OR v_FullCollectionMembership.SiteCode='SF1' OR v_FullCollectionMembership.SiteCode='SG1' OR v_FullCollectionMembership.SiteCode='SM1')
                when 'WBR' Then (where v_FullCollectionMembership.SiteCode='CO1' OR v_FullCollectionMembership.SiteCode='CP1' OR v_FullCollectionMembership.SiteCode='LS1' OR v_FullCollectionMembership.SiteCode='NV1' OR v_FullCollectionMembership.SiteCode='SR1')
            end
            --SCAH
            --where v_FullCollectionMembership.SiteCode='VN1'
            --CCS
            --where v_FullCollectionMembership.SiteCode='SS1' OR v_FullCollectionMembership.SiteCode='SC1' 
            --CVR
            --where v_FullCollectionMembership.SiteCode='MM1' OR v_FullCollectionMembership.SiteCode='MM2'
            --EBR
            --where v_FullCollectionMembership.SiteCode='AB1' OR v_FullCollectionMembership.SiteCode='DM1' OR v_FullCollectionMembership.SiteCode='EM1'
            --PCR
            --where v_FullCollectionMembership.SiteCode='CMB' OR v_FullCollectionMembership.SiteCode='CUC' OR v_FullCollectionMembership.SiteCode='MP1' OR v_FullCollectionMembership.SiteCode='MP2' OR v_FullCollectionMembership.SiteCode='PA1' OR v_FullCollectionMembership.SiteCode='PAF' OR v_FullCollectionMembership.SiteCode='PDU' OR v_FullCollectionMembership.SiteCode='PLA' OR v_FullCollectionMembership.SiteCode='PMF' OR v_FullCollectionMembership.SiteCode='PMO' OR v_FullCollectionMembership.SiteCode='PMP' OR v_FullCollectionMembership.SiteCode='POR' OR v_FullCollectionMembership.SiteCode='PPV' OR v_FullCollectionMembership.SiteCode='PRS' OR v_FullCollectionMembership.SiteCode='PSA' OR v_FullCollectionMembership.SiteCode='SH1' OR v_FullCollectionMembership.SiteCode='SZ1' OR v_FullCollectionMembership.SiteCode='SZ2'
            --SSR
            --where v_FullCollectionMembership.SiteCode='AM1' OR v_FullCollectionMembership.SiteCode='NM1' OR v_FullCollectionMembership.SiteCode='SF1' OR v_FullCollectionMembership.SiteCode='SG1' OR v_FullCollectionMembership.SiteCode='SM1'
            --WBR
            --where v_FullCollectionMembership.SiteCode='CO1' OR v_FullCollectionMembership.SiteCode='CP1' OR v_FullCollectionMembership.SiteCode='LS1' OR v_FullCollectionMembership.SiteCode='NV1' OR v_FullCollectionMembership.SiteCode='SR1'
    ) MEMB on MEMB.ResourceID = v_GS_INSTALLED_SOFTWARE_CATEGORIZED.ResourceID 
    INNER JOIN v_GS_COMPUTER_SYSTEM on v_GS_COMPUTER_SYSTEM.ResourceID = MEMB.ResourceID 

    where v_GS_INSTALLED_SOFTWARE_CATEGORIZED.SoftwareID = '76F63E67-3A8F-4675-9FB1-08A3E40CE9FE'
    --where v_GS_INSTALLED_SOFTWARE_CATEGORIZED.SoftwareID = @SoftwareID
    Order by MEMB.Netbios_Name0 ASC

2 个答案:

答案 0 :(得分:0)

您不能发出where声明:

  (case when @test = 'SCAH' and v_FullCollectionMembership.SiteCode='VN1' then 'true'
        when @test = 'CCS' and e v_FullCollectionMembership.SiteCode in ('SS1', 'SC1') 
        . . .
  end) = 'true'

我建议将其移至where子句,而不是试图将其保留在on子句中。如果它保留在on子句中,则需要在其前面加and(或or,具体取决于逻辑)。

答案 1 :(得分:0)

如果要执行“selective where”子句,还可以使用动态sql。


declare @sql varchar(max)
set @sql = 'select * from table '
if( @regions = '??')
  set @sql = @sql + ' where ....'
else if( @regions = '???' )
  set @sql = @sql + ' where ...'
exec(@sql)