SQL:根据其他列更新列

时间:2011-09-07 19:56:29

标签: sql-server variables sql-update

SQL Server: 我有一张桌子。

ID Type1 Type2 error
_____________________
1   P107  0057   NULL
2   P101  1142   NULL
3   P107  1142   NULL

现在我必须检查表1中的type1列,如果数据存在,则表2中的type2。 让我们假设P101存在于table1中,而0057存在于table2中。 Table1有很多列但我们正在检查col ='type1'同样适用于table2只检查col ='type2'。  因此,生成的错误应该是:

ID Type1 Type2 error
_____________________
1   P107  0057   <type1 invalid>
2   P101  1142   <type2 invalid>
3   P107  1142   <type1 invalid> + <type2 invalid>

PS:错误列中可能还有其他消息已存在,我们不想删除这些消息,而是添加新消息(如果有的话)。 例如,我可能有这样的表:

ID Type1 Type2 error
_____________________
1   P107  0057   NULL
2   P101  1142   <duplicate>
3   P107  1142   NULL

现在,我想在id = 2中添加新错误,如

ID Type1 Type2 error
_____________________
1   P107  0057   <type1 invalid>
2   P101  1142   <duplicate> + <type2 invalid>
3   P107  1142   <type1 invalid> + <type2 invalid>

任何帮助将不胜感激!提前谢谢。

2 个答案:

答案 0 :(得分:3)

如果以下内容产生您想要的结果:

SELECT t.ID, t.Type1, t.Type2, 
  error = COALESCE(t.error + ' + ', '') + CASE
  WHEN t1.type1 IS NULL AND t2.type2 IS NULL 
    THEN '<type1 invalid> + <type2 invalid>'
  WHEN t1.type1 IS NULL THEN '<type1 invalid>'
  WHEN t2.type2 IS NULL THEN '<type2 invalid>'
  ELSE t.error END
FROM dbo.table AS t
LEFT OUTER JOIN dbo.Table1 AS t1
ON t.Type1 = t1.Type1
LEFT OUTER JOIN dbo.Table2 AS t2
ON t.Type2 = t2.Type2
WHERE t1.Type1 IS NULL OR t2.Type2 IS NULL;

然后您可以运行此更新语句:

UPDATE t
SET error = COALESCE(t.error + ' + ', '') + CASE
  WHEN t1.type1 IS NULL AND t2.type2 IS NULL 
    THEN '<type1 invalid> + <type2 invalid>'
  WHEN t1.type1 IS NULL THEN '<type1 invalid>'
  WHEN t2.type2 IS NULL THEN '<type2 invalid>'
  ELSE t.error END
FROM dbo.table AS t
LEFT OUTER JOIN dbo.Table1 AS t1
ON t.Type1 = t1.Type1
LEFT OUTER JOIN dbo.Table2 AS t2
ON t.Type2 = t2.Type2
WHERE t1.Type1 IS NULL OR t2.Type2 IS NULL;

添加了where子句,以便在事后添加到问题中的新信息。当没有错误消息时,假设该列为NULL

编辑已针对 MORE 新要求进行了更新。

再次

编辑。有回音吗?

这是一个你可以在tempdb中运行的repro,看看,不,你不需要使用变量而不,你不需要写10! (是的,我知道什么是因子意味着)案例表达式来完成这个。

核心表:

USE tempdb;
GO

CREATE TABLE dbo.[table]
(
    ID INT,
    Type1 VARCHAR(4),
    Type2 VARCHAR(4),
    Type3 VARCHAR(4),
    Type4 VARCHAR(4),
    Type5 VARCHAR(4),
    Type6 VARCHAR(4),
    Type7 VARCHAR(4),
    Type8 VARCHAR(4),
    Type9 VARCHAR(4),
    Type10 VARCHAR(4),
    error VARCHAR(MAX)
);
GO

有些行:

INSERT dbo.[table] SELECT -- this will yield type 1 invalid:
 1,'P107','0057','x',   'x','x','x','x',   'x','x','x',NULL
UNION ALL SELECT          -- this will yield type 2 invalid:
 2,'P101','1142','x',   'x','x','x','x',   'x','x','x','<duplicate>'
UNION ALL SELECT          -- this will yield type 1 + type 2 invalid:
 3,'P107','1142','x',   'x','x','x','x',   'x','x','x',NULL
UNION ALL SELECT          -- no problems here:
 4,'x',   'x',   'x205','x','x','x','y676','x','x','x',NULL
UNION ALL SELECT          -- this will yield type 3 invalid:
 5,'x',   'x',   'x206','x','x','x','y676','x','x','x','<other>';

相关表格和行:

CREATE TABLE dbo.Table1 (Type1  VARCHAR(4));
CREATE TABLE dbo.Table2 (Type2  VARCHAR(4));
CREATE TABLE dbo.Table3 (Type3  VARCHAR(4));
CREATE TABLE dbo.Table4 (Type4  VARCHAR(4));
CREATE TABLE dbo.Table5 (Type5  VARCHAR(4));
CREATE TABLE dbo.Table6 (Type6  VARCHAR(4));
CREATE TABLE dbo.Table7 (Type7  VARCHAR(4));
CREATE TABLE dbo.Table8 (Type8  VARCHAR(4));
CREATE TABLE dbo.Table9 (Type9  VARCHAR(4));
CREATE TABLE dbo.Table10(Type10 VARCHAR(4));

INSERT dbo.Table1 SELECT 'P101';
INSERT dbo.Table2 SELECT '0057';
INSERT dbo.Table3 SELECT 'x205';
INSERT dbo.Table7 SELECT 'y676';

-- I know you won't have x's but I assume you'll have
-- valid data most of the time.

INSERT dbo.Table1  SELECT 'x';
INSERT dbo.Table2  SELECT 'x';
INSERT dbo.Table3  SELECT 'x';
INSERT dbo.Table4  SELECT 'x';
INSERT dbo.Table5  SELECT 'x';
INSERT dbo.Table6  SELECT 'x';
INSERT dbo.Table7  SELECT 'x';
INSERT dbo.Table8  SELECT 'x';
INSERT dbo.Table9  SELECT 'x';
INSERT dbo.Table10 SELECT 'x';

让我们确保表格看起来正确:

SELECT * FROM dbo.[table];

现在,一个语句,只有10个案例表达式,而不是10个!:

UPDATE t SET error = REPLACE(COALESCE(t.error, '') 
    + CASE WHEN t1.type1   IS NULL THEN '<type1 invalid>'  ELSE '' END
    + CASE WHEN t2.type2   IS NULL THEN '<type2 invalid>'  ELSE '' END
    + CASE WHEN t3.type3   IS NULL THEN '<type3 invalid>'  ELSE '' END
    + CASE WHEN t4.type4   IS NULL THEN '<type4 invalid>'  ELSE '' END
    + CASE WHEN t5.type5   IS NULL THEN '<type5 invalid>'  ELSE '' END
    + CASE WHEN t6.type6   IS NULL THEN '<type6 invalid>'  ELSE '' END
    + CASE WHEN t7.type7   IS NULL THEN '<type7 invalid>'  ELSE '' END
    + CASE WHEN t8.type8   IS NULL THEN '<type8 invalid>'  ELSE '' END
    + CASE WHEN t9.type9   IS NULL THEN '<type9 invalid>'  ELSE '' END
    + CASE WHEN t10.type10 IS NULL THEN '<type10 invalid>' ELSE '' END, '><', '> + <')
FROM dbo.[table] AS t
LEFT OUTER JOIN dbo.Table1  AS t1  ON t.Type1  = t1.Type1
LEFT OUTER JOIN dbo.Table2  AS t2  ON t.Type2  = t2.Type2
LEFT OUTER JOIN dbo.Table3  AS t3  ON t.Type3  = t3.Type3
LEFT OUTER JOIN dbo.Table4  AS t4  ON t.Type4  = t4.Type4
LEFT OUTER JOIN dbo.Table5  AS t5  ON t.Type5  = t5.Type5
LEFT OUTER JOIN dbo.Table6  AS t6  ON t.Type6  = t6.Type6
LEFT OUTER JOIN dbo.Table7  AS t7  ON t.Type7  = t7.Type7
LEFT OUTER JOIN dbo.Table8  AS t8  ON t.Type8  = t8.Type8
LEFT OUTER JOIN dbo.Table9  AS t9  ON t.Type9  = t9.Type9
LEFT OUTER JOIN dbo.Table10 AS t10 ON t.Type10 = t10.Type10
WHERE t1.Type1   IS NULL
   OR t2.Type2   IS NULL
   OR t3.Type3   IS NULL
   OR t4.Type4   IS NULL
   OR t5.Type5   IS NULL
   OR t6.Type6   IS NULL
   OR t7.Type7   IS NULL
   OR t8.Type8   IS NULL
   OR t9.Type9   IS NULL
   OR t10.Type10 IS NULL;
GO

更新运行后,让我们检查并确保更新了正确的行:

SELECT * FROM dbo.[table]; 
GO

清理:

DROP TABLE dbo.[table],
    dbo.Table1, dbo.Table2, dbo.Table3, dbo.Table4, dbo.Table5, 
    dbo.Table6, dbo.Table7, dbo.Table8, dbo.Table9, dbo.Table10;

答案 1 :(得分:0)

UPDATE yt
    SET error = CASE WHEN t1.Type1 IS NULL AND t2.Type2 IS NULL THEN '<type1 invalid> + <type2 invalid>'
                     WHEN t1.Type1 IS NULL THEN '<type1 invalid>'
                     WHEN t2.Type2 IS NULL THEN '<type2 invalid>'
                END
    FROM YourTable yt
        LEFT JOIN table1 t1
            ON yt.Type1 = t1.Type1
        LEFT JOIN table2 t2
            ON yt.Type2 = t2.Type2
    WHERE t1.Type1 IS NULL 
        OR t2.Type2 IS NULL;