如何处理主键违规 [英] how to handle primary key violation

查看:102
本文介绍了如何处理主键违规的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

这是我的存储过程
我必须在其中放置主键的条件
因为在我的应用程序中发生主键冲突错误

hi to all!

here is my stored procedure
where i shall have to put the condition for primary key
becouse in my application primary key violation error occur

procedure [dbo].[proc_module_info] (@mid nvarchar(50),@mname nvarchar (50), @uip nvarchar (50), @login nvarchar (50))
as
INSERT INTO [GCERP_SEC].[dbo].[sec_modules]
           ([modid_int]
           ,[modname_vc]
           ,[createdby_vc]
           ,[creationdate_dt]
           ,[modifiedby_vc]
           ,[modificationdate_dt]
           ,[ipadd_vc]
           ,[pcname_vc])

     VALUES
           (@mid, @mname, @login, GETDATE(), @login, GETDATE(),@uip, HOST_NAME())

推荐答案

IF NOT EXISTS(SELECT * FROM [dbo].[sec_modules] WHERE [modid_int] = @mid )
BEGIN
    -- Put your insert statement here

END


请注意,在mySQL中,您可以
As a note, in mySQL you can do
insert into `table` values ( ... )
on duplicate key update ...



这样,您就可以插入不存在的行,并更新除键之外的所有其他字段.不过,我不确定MS SQL中是否有等效功能.并且从您的列名中,您需要创建统计信息,因此在这种情况下,您还是不想这样做.



That allows you to insert a row if it doesn''t already exist, and update all fields except the key if it does. I''m not sure if there is an equivalent in MS SQL, though. And from your column names, you want the creation statistics, so you wouldn''t want to do that anyway in this case.


您好,
试试这个-
Hi,
Try this-
IF NOT EXISTS(SELECT * FROM [dbo].[sec_modules] WHERE [modid_int] = @mid )
BEGIN
   --Here will be your insert command
END


这篇关于如何处理主键违规的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆