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

4

IF 块的整体原因(业务逻辑)尚不清楚。

为什么不创建一个名为#Map 的更新(补丁)表并进行更新而不考虑匹配。下面的 TSQL 将 #Map 连接到代码上的原始表以更新描述。

-- Patching table
Create table #Map
(
  Desc1 varchar(128),
  Code1 varchar(16)
);

-- Add data to table
insert into #Map values
('ComAutoPhysDam  -  Garagekeepers (Comprehensive)', 'GA_GKOTC'),
('ComAutoPhysDam  -  GarageDealers (Comprehensive) - Autos in Building', 'GA_ABLDG'),
('ComAutoPhysDam  -  GarageDealers Comprehensive - Std Open Lots', 'GA_LOTS'),
('ComAutoPhysDam  -  GarageDealers Comprehensive - Non-Std Lots', 'GA_NLOTS'),
('ComAutoPhysDam  -  GarageDealers Comprehensive - Misc Bldg', 'GA_MISC'),
('ComAutoPhysDam - Garagekeepers (Collision)', 'GA_GKCOL'),
('ComAutoPhysDam - Garage Dealers Collision', 'GA_BLNKC');

-- Show data in table
select * from #Map;

-- Update regardless
UPDATE SIU_CoverageMap
SET sCM_CoverageDesc = m.Desc1
FROM SIU_CoverageMap c join #Map m on c.sCM_Code = m.Code1
于 2013-08-21T20:30:47.247 回答