作者:百变精灵2596 | 来源:互联网 | 2023-05-18 04:32
Imtryingtoinsertintoatablethathasuniqueconstrainton(ProductType,ProductOwnerid)inaw
I'm trying to insert into a table that has unique constraint on (ProductType, ProductOwnerid) in a way that when that key already exists that the contraint violation would not be triggered. So I have this SQL that works as intended:
我正在尝试插入一个对(ProductType,ProductOwnerid)具有唯一约束的表,其方式是当该键已经存在时,不会触发约束违规。所以我有这个SQL按预期工作:
INSERT INTO dbo.Products (ProductType, ProductOwnerId)
SELECT TOP 1 22, 44 FROM dbo.products prods
WHERE NOT EXISTS (
SELECT prods2.ProductType FROM dbo.products prods2
WHERE prods2.ProductType = 22 AND prods2.ProductOwnerId = 44)
Is this decent SQL or how could I improve this? I'm not a big fan of the TOP 1, how can I make this more readable/better performing?
这是不错的SQL还是我怎么能改进这个?我不是TOP 1的忠实粉丝,我怎样才能让它更具可读性/表现更好?
2 个解决方案