如果值pk(customer_code)中的值不等于值,我如何插入值? [英] How I insert values if value not equal value in column pk(customer_code) ?
本文介绍了如果值pk(customer_code)中的值不等于值,我如何插入值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写这段代码,但我有错误(违反PRIMARY KEY约束'PK_Customers'。无法在对象'dbo.Customers'中插入重复键。重复键值为(11018)。
该声明已被终止。
)
i write this code but i have error (Violation of PRIMARY KEY constraint 'PK_Customers'. Cannot insert duplicate key in object 'dbo.Customers'. The duplicate key value is (11018).
The statement has been terminated.
)
insert into Customers(customer_code, customer_name,RgCode,CityCode,Phone,Distriet,Street)
select 11018, 'any name', 5 , 8 , 0516188, 0795526 ,'title' from Customers
where not exists(select customer_code from Customers where customer_code = 11018)
我有什么尝试过:
i首先要检查值pk是否找到,如果找不到,如果找不到则更新
What I have tried:
i want check first if the value pk is finding or not, if is it finding he insert if not you update if find change in the values
推荐答案
试试这个:
Try this:
UPDATE Customers SET ... WHERE customer_code = 11018;
IF @@ROWCOUNT = 0
BEGIN
INSERT INTO Customers (...) VALUES ( ...)
END
将它放入存储过程并传递值。
Put that into an stored procedure and pass it the values.
这篇关于如果值pk(customer_code)中的值不等于值,我如何插入值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文