1

そのため、いくつかの場所で列を変更する更新スクリプトを作成しており、「IF NOT EXISTS」ステートメントを多数使用するよりも、このコードを要約するためのはるかに優れた方法を見つけようとしています。

これは問題の現在のコードであり、CASE ステートメントの方が優れているかどうか、またはこれをよりクリーンにする方法がわかりませんか?

これは、効率的なクエリの最適化と一般的な使用の両方の学習経験になります。:)

IF NOT EXISTS (
        SELECT sCM_CoverageDesc
        FROM SIU_CoverageMap
        WHERE sCM_CoverageDesc = 'ComAutoPhysDam  -  Garagekeepers (Comprehensive)')
BEGIN
    UPDATE SIU_CoverageMap
    SET sCM_CoverageDesc = 'ComAutoPhysDam  -  Garagekeepers (Comprehensive)'
    FROM SIU_CoverageMap
    WHERE sCM_Code = 'GA_GKOTC'
END

IF NOT EXISTS (
        SELECT sCM_CoverageDesc
        FROM SIU_CoverageMap
        WHERE sCM_CoverageDesc = 'ComAutoPhysDam  -  GarageDealers (Comprehensive) - Autos in Building')
BEGIN
    UPDATE SIU_CoverageMap
    SET sCM_CoverageDesc = 'ComAutoPhysDam  -  GarageDealers (Comprehensive) - Autos in Building'
    FROM SIU_CoverageMap
    WHERE sCM_Code = 'GA_ABLDG'
END

IF NOT EXISTS (
        SELECT sCM_CoverageDesc
        FROM SIU_CoverageMap
        WHERE sCM_CoverageDesc = 'ComAutoPhysDam  -  GarageDealers Comprehensive - Std Open Lots')
BEGIN
    UPDATE SIU_CoverageMap
    SET sCM_CoverageDesc = 'ComAutoPhysDam  -  GarageDealers Comprehensive - Std Open Lots'
    FROM SIU_CoverageMap
    WHERE sCM_Code = 'GA_LOTS'
END

IF NOT EXISTS (
        SELECT sCM_CoverageDesc
        FROM SIU_CoverageMap
        WHERE sCM_CoverageDesc = 'ComAutoPhysDam  -  GarageDealers Comprehensive - Non-Std Lots')
BEGIN
    UPDATE SIU_CoverageMap
    SET sCM_CoverageDesc = 'ComAutoPhysDam  -  GarageDealers Comprehensive - Non-Std Lots'
    FROM SIU_CoverageMap
    WHERE sCM_Code = 'GA_NLOTS'
END

IF NOT EXISTS (
        SELECT sCM_CoverageDesc
        FROM SIU_CoverageMap
        WHERE sCM_CoverageDesc = 'ComAutoPhysDam  -  GarageDealers Comprehensive - Misc Bldg')
BEGIN
    UPDATE SIU_CoverageMap
    SET sCM_CoverageDesc = 'ComAutoPhysDam  -  GarageDealers Comprehensive - Misc Bldg'
    FROM SIU_CoverageMap
    WHERE sCM_Code = 'GA_MISC'
END

IF NOT EXISTS (
        SELECT sCM_CoverageDesc
        FROM SIU_CoverageMap
        WHERE sCM_CoverageDesc = 'ComAutoPhysDam - Garagekeepers (Collision)')
BEGIN
    UPDATE SIU_CoverageMap
    SET sCM_CoverageDesc = 'ComAutoPhysDam - Garagekeepers (Collision)'
    FROM SIU_CoverageMap
    WHERE sCM_Code = 'GA_GKCOL'
END

IF NOT EXISTS (
        SELECT sCM_CoverageDesc
        FROM SIU_CoverageMap
        WHERE sCM_CoverageDesc = 'ComAutoPhysDam - Garage Dealers Collision')
BEGIN
    UPDATE SIU_CoverageMap
    SET sCM_CoverageDesc = 'ComAutoPhysDam - Garage Dealers Collision'
    FROM SIU_CoverageMap
    WHERE sCM_Code = 'GA_BLNKC'
END
ELSE
    PRINT ('Table has already been updated.')
4

1 に答える 1