我在SQL中多次执行后停止插入数据



我必须通过插入表来生成数据,但每次我执行这个PS时,它都会在表的末尾添加新行,我如何才能在多次执行

后停止插入数据我的代码是:
DECLARE @CountFID int 
SET @CountFID = (SELECT COUNT(FID) FROM DataFoodView)
INSERT INTO FoodSara_tbl
SELECT FID + (@CountFID) , Fname, Ftype, Fcount ,FoodDate , Fregion, Fdescription
FROM dbo.DataFoodView
ORDER BY FoodDate
INSERT INTO FoodSara_tbl
SELECT FID + 2*(@CountFID), Fname, Ftype, Fcount ,FoodDate , Fregion, Fdescription
FROM dbo.DataFoodView
ORDER BY FoodDate

PS:我不想使用触发器

如果我得到你的问题,你可能需要使用where来检查是否存在在你的表:

DECLARE @CountFID int 
SET @CountFID = (SELECT COUNT(FID) FROM DataFoodView)
INSERT INTO FoodSara_tbl
SELECT FID + (@CountFID) , Fname, Ftype, Fcount ,FoodDate , Fregion, Fdescription
FROM dbo.DataFoodView
Where FID not in (select FID from FoodSara_tbl )
INSERT INTO FoodSara_tbl
SELECT FID + 2*(@CountFID), Fname, Ftype, Fcount ,FoodDate , Fregion, Fdescription
FROM dbo.DataFoodView
Where FID not in (select FID from FoodSara_tbl )

最新更新