且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何防止在sql server中插入重复的id

更新时间:2022-02-25 22:53:55

防止SQL表中重复值的***方法是创建唯一的密钥使用正确的列...

http://www.w3schools.com/sql/ sql_unique.asp [ ^ ]

这是SQL应该工作的方式,而不是通过在存储过程中编写任何代码...只记得存储过程可以绕过唯一约束不能 - 永远!
The best way to prevent duplicate values in SQL table is to create unique keys using the proper column...
http://www.w3schools.com/sql/sql_unique.asp[^]
This is the way SQL should work and not by writing any code in stored procedure...Just remember that stored procedure can be bypassed where unique constrains can not - ever!


创建此存储过程...您将获得解决方案



Create this Stored procedure ... You will get your Solution

create procedure sp_by_aarif
@item_id int=0,
@item_name varchar(50)= ''
AS
BEGIN
   if exists (select * from yourTableName where item_id=@item_id)
       begin
            Insert into yourTableName values (@item_id,@item_name)
       end

end







注意:这里给你的表名而不是yourTableName



谢谢

AARIF SHAIKH




Note: Here give your Table name rather than "yourTableName"

Thanks
AARIF SHAIKH


INSERT INTO issuemaster
    SELECT
        serialid
    FROM
        serialmaster S
    WHERE
        NOT EXISTS (
            SELECT
                serialId
            FROM
                issuemaster I
            WHERE
                S.serialid = I.serialid)