gpt4 book ai didi

SQL 错误 : "There is already an object named XXXX in the database"

转载 作者:行者123 更新时间:2023-12-02 07:13:25 25 4
gpt4 key购买 nike

这是我的查询。我想要做的是每周运行此查询,以便表 PlanFinder.InvalidAwps 将有新记录。但是当我运行查询时,它给了我这个错误:

There is already an object named 'InvalidAwps' in the database. 

我无法更改表名。它必须保持不变。那么我如何才能每周运行此查询并保持表名不变呢?

-------------------------------------
IF EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[PlanFinder].[InvalidAwps]')
AND type in (N'U'))
BEGIN
DROP TABLE [PlanFinder].[InvalidAwps]
END

SELECT DISTINCT P.Ndc Ndc, A.Price AwpUnitCost INTO PlanFinder.InvalidAwps
FROM
PlanFinder.PlanFinder.HpmsFormulary P
LEFT JOIN (SELECT Ndc, Price FROM MHSQL01D.Drug.FdbPricing.vNdcPrices
WHERE PriceTypeCode = '01' AND CurrentFlag = 1) A
ON P.Ndc = A.Ndc
WHERE (A.Ndc IS NULL OR A.Price <= 0 OR A.Price IS NULL)
AND p.Ndc IS NOT NULL
----------------------------------------------

最佳答案

为什么每次都要经历删除和重新创建表的工作?相反,创建表一次,然后继续:

truncate table [PlanFinder].[InvalidAwps]
go

insert into [PlanFinder].[InvalidAwps]
(Ndc, AwpUnitCost)
SELECT DISTINCT P.Ndc Ndc, A.Price AwpUnitCost
FROM
PlanFinder.PlanFinder.HpmsFormulary P
LEFT JOIN (SELECT Ndc, Price FROM MHSQL01D.Drug.FdbPricing.vNdcPrices
WHERE PriceTypeCode = '01' AND CurrentFlag = 1) A
ON P.Ndc = A.Ndc
WHERE (A.Ndc IS NULL OR A.Price <= 0 OR A.Price IS NULL)
AND p.Ndc IS NOT NULL

关于SQL 错误 : "There is already an object named XXXX in the database",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3535224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com