所以我正在编写一个更新脚本来更改几列中的列,我试图找到一种更好的方法来压缩这些代码而不是拥有一堆“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.')
答案 0 :(得分: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