我有一个表的父类和子类。我已经能够编写合并查询以在输出表中插入唯一类别。但是我在如何用父母替换孩子的cat_id以使他们与他们联系时遇到困难。
CREATE TABLE import_desctemp(
TempID INT NOT NULL PRIMARY KEY
,Main nvarchar(10) NOT NULL
,Sub1 nvarchar(10)
,Sub2 nvarchar(10)
,cst INT
);
INSERT INTO import_desctemp(TempID,Main,Sub1,Sub2,cst) VALUES (TempID,'Main','Sub1','Sub2',cst);
INSERT INTO import_desctemp(TempID,Main,Sub1,Sub2,cst) VALUES (1,'Commercial','Bathroom',NULL,NULL);
INSERT INTO import_desctemp(TempID,Main,Sub1,Sub2,cst) VALUES (2,'Commercial','Kitchen','Pantry',NULL);
INSERT INTO import_desctemp(TempID,Main,Sub1,Sub2,cst) VALUES (3,'Commercial','Kitchen','Pantry',NULL);
INSERT INTO import_desctemp(TempID,Main,Sub1,Sub2,cst) VALUES (4,'Commercial','Rumpus',NULL,NULL);
INSERT INTO import_desctemp(TempID,Main,Sub1,Sub2,cst) VALUES (5,'Commercial','Rumpus',NULL,NULL);
INSERT INTO import_desctemp(TempID,Main,Sub1,Sub2,cst) VALUES (6,'Commercial',NULL,NULL,NULL);
INSERT INTO import_desctemp(TempID,Main,Sub1,Sub2,cst) VALUES (7,'Commercial',NULL,NULL,NULL);
INSERT INTO import_desctemp(TempID,Main,Sub1,Sub2,cst) VALUES (8,'Commercial',NULL,NULL,NULL);
INSERT INTO import_desctemp(TempID,Main,Sub1,Sub2,cst) VALUES (9,'Domestic','Bathroom','Toilet',NULL);
INSERT INTO import_desctemp(TempID,Main,Sub1,Sub2,cst) VALUES (10,'Domestic','Bathroom','Toilet',NULL);
INSERT INTO import_desctemp(TempID,Main,Sub1,Sub2,cst) VALUES (11,'Domestic','Bathroom','Toilet',NULL);
INSERT INTO import_desctemp(TempID,Main,Sub1,Sub2,cst) VALUES (12,'Domestic','Bathroom',NULL,NULL);
INSERT INTO import_desctemp(TempID,Main,Sub1,Sub2,cst) VALUES (13,'Domestic','Kitchen',NULL,NULL);
INSERT INTO import_desctemp(TempID,Main,Sub1,Sub2,cst) VALUES (14,'Domestic','Kitchen',NULL,NULL);
INSERT INTO import_desctemp(TempID,Main,Sub1,Sub2,cst) VALUES (15,'Domestic','Laundry',NULL,NULL);
INSERT INTO import_desctemp(TempID,Main,Sub1,Sub2,cst) VALUES (16,'Domestic','Laundry',NULL,NULL);
INSERT INTO import_desctemp(TempID,Main,Sub1,Sub2,cst) VALUES (17,'Domestic','Laundry',NULL,NULL);
INSERT INTO import_desctemp(TempID,Main,Sub1,Sub2,cst) VALUES (18,'Domestic',NULL,NULL,NULL);
INSERT INTO import_desctemp(TempID,Main,Sub1,Sub2,cst) VALUES (19,'Domestic',NULL,NULL,NULL);
CREATE TABLE Description_Category(
CSD_ID INT NOT NULL PRIMARY KEY
,Category nvarchar(10)
,Parent_ID INT
,Primary_cat_id INT
);
INSERT INTO Description_Category(CSD_ID,Category,Parent_ID,Primary_cat_id) VALUES (0,NULL,NULL,NULL);
这是我写的代码。它可以将 import_desctemp 表中的类别插入 Description_Category 只是在将正确的ID插入Primary_cat_ID时遇到问题,以便它引用Parent类。
--ALTER TABLE import_desctemp ADD CST int
DECLARE @MergeOutput1 table
(
ActionType NVARCHAR(10),
csd INT,
cat nvarchar(10))
MERGE Description_Category AS C
USING (
SELECT Main -- Do a group by to eliminate duplicates
FROM import_desctemp I
WHERE Main IS NOT null
GROUP BY Main
) AS SRC
ON (1=0)
WHEN NOT MATCHED BY TARGET THEN
-- Insert Categories from excel table to live table
INSERT (category)
VALUES (Main)
OUTPUT
$ACTION,
INSERTED.CSD_ID,
INSERTED.category
INTO @MergeOutput1;
MERGE Description_Category AS C
USING (
SELECT DISTINCT Main, Sub1
FROM import_desctemp
WHERE sub1 is not null
GROUP BY Main, Sub1
) AS SRC
ON (1=0)
WHEN NOT MATCHED BY TARGET THEN
-- Insert Categories from excel table to live table
INSERT (Category)
VALUES (Sub1)
OUTPUT
$ACTION,
INSERTED.CSD_ID,
INSERTED.Category
INTO @MergeOutput1;
;WITH cte AS (
SELECT t.csd, t.cat--, c.csd_id as cat_id
FROM @MergeOutput1 t
INNER JOIN import_desctemp c
ON c.Main = t.cat
INNER JOIN import_desctemp c1
ON c1.Sub1 = t.cat
)
UPDATE R
SET primary_Cat_id = c.csd
FROM Description_Category R
INNER JOIN cte c
ON c.csd = R.CSD_ID
/*--Insert primary category IDs from @table into the template category table
update Description_Category
SET Primary_Cat_Id = M.csd
FROM @MergeOutput1 M
JOIN Description_Category O ON O.CSD_ID = M.csd*/
SELECT * FROM @MergeOutput1
/*insert Description_Category (Category)
OUTPUT INSERTED.CSD_ID
select sub1
FROM import_desctemp p
left join @MergeOutput1 m on p.Main = m.cat--LEFT join is important, otherwise may not copy all rows
WHERE p.Sub1 IS NOT NULL*/
/*--Insert cst IDs from @table into the excel table
update import_desctemp
SET CST = M.csd
FROM @MergeOutput1 M
JOIN import_desctemp O ON O.Main = M.Cat
COMMIT;*/
© 2016 Microsoft Terms Privacy & cookies Developers English (United States)
预期结果
+--------+------------+-----------+----------------+
| TempID | Category | Parent_id | Primary_cat_id |
+--------+------------+-----------+----------------+
| 1 | Commercial | 0 | 1 |
| 12 | Domestic | 0 | 12 |
| 16 | Bathroom | 1 | 1 |
| 17 | Kitchen | 1 | 1 |
| 18 | Rumpus | 1 | 1 |
| 19 | Bathroom | 12 | 12 |
| 20 | Kitchen | 12 | 12 |
| 21 | Laundry | 12 | 12 |
+--------+------------+-----------+----------------+
答案 0 :(得分:0)
您需要将表格与我的<>c__DisplayClass1
类别(我称之为categories
)ID一起加入:
Table
更新:
SELECT c1.pkid, t.sub1, c.pkid
FROM Table t
INNER JOIN categories c
ON c.category = t.main
INNER JOIN categories c1
ON c1.category = t.sub