因此,我正在编写一个更新脚本来更改几个位置的列,并且我试图找到一种比拥有一堆“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.')