1

私は病院グループで働いており、そこには複数のサイト サーバーがあり、約 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
4

2 に答える 2

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)

于 2013-03-19T23:23:02.973 に答える
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ロジックによっては ) を付ける必要があります。

于 2013-03-19T23:19:33.477 に答える