0

这是我们遇到的情况的模拟:

IF OBJECT_ID('TEMPDB..#People') IS NOT NULL BEGIN DROP TABLE #People END;
CREATE TABLE #People
    (
    Name VARCHAR(100),
    Category VARCHAR(20),
    ID INT
    );
INSERT INTO #People
    values
    ('x','Bronze',1),
    ('y','Bronze',2),
    ('z','Silver',3),
    ('j','Gold',4),
    ('q','Bronze',5),
    ('x','Silver',1);


IF OBJECT_ID('TEMPDB..#Category') IS NOT NULL BEGIN DROP TABLE #Category END;
CREATE TABLE #Category
    (
    Category VARCHAR(100)
    );
INSERT INTO #Category
    values
    ('Gold'),
    ('Silver'),
    ('Bronze');

如果 aname没有,Category例如x没有,Gold那么我希望创建一行并将其添加到表中#People,其ID值为 -1。

我目前的解决方案是这样的:

WITH x AS
    (
    SELECT  DISTINCT
            x.Name,
            s.Category
    FROM    #People x
            CROSS JOIN #Category s
    )
INSERT  INTO    #People
SELECT  J.Name,
        J.Category,
        ID = -1
FROM    x J
WHERE   NOT EXISTS
        (
        SELECT  1
        FROM    #People Q
        WHERE   J.Name = Q.Name
                AND J.Category = Q.Category
        );

看看它的工作原理!...:

SELECT *
FROM #People;

我有一种感觉CROSS APPLY可能是一个很好的运算符来简化上述操作 - 查找、创建和插入这些行的最简单方法是什么?

4

1 回答 1

1
insert into People(Name, Category, Id)
(
select distinct
  p.Name, 
  c.Category, 
  p.Id 
from 
        people p 
  cross join category c
where
  Concat(p.id, c.Category) not in (select Concat(id, Category) from people)
);

http://www.sqlfiddle.com/#!6/92cd5/14

于 2013-09-04T12:43:01.960 回答