我有一个大表,我想从该表中获取数据,并将其插入其他(较小的)表中。但是在执行此操作时,我想将大表(“已处理”)中的列标记为 true,以便下次运行查询时,它不会最终创建重复项。除了事后创建小查询以将所有内容设置为 0 到 1 之外,还有更简单的方法吗?或者这是最有效的方法?
DECLARE @largeTable TABLE(
[Id] [int] IDENTITY(1,1) NOT NULL,
productName varchar(100),
productId int,
quantity int,
someVar int,
Processed bit)
INSERT INTO @largeTable (productName, productId, quantity, someVar, Processed)
VALUES ('Apple', 1, 50, 34, 0),
('Orange', 2, 50, 53, 0),
('Banana', 3, 50, 165, 0),
('Pineapple', 4, 74, 34, 0),
('Mango', 5, 50, 388, 0),
('Kiwi', 6, 50, 2, 0),
('Strawberry', 7, 50, 16, 0)
SELECT * FROM @largeTable
DECLARE @smallTable TABLE(
[Id] [int] IDENTITY(1,1) NOT NULL,
productName varchar(100),
productId int)
INSERT INTO @smallTable (productName, productId)
SELECT productName, productId
FROM @largeTable
WHERE Processed = 0
UPDATE @largeTable
Set Processed = 1
WHERE Processed = 0
SELECT * FROM @largeTable
SELECT * FROM @smallTable