在将数据插入数据库时​​避免重复 [英] Avoiding Duplicates while inserting data into data base

查看:56
本文介绍了在将数据插入数据库时​​避免重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在做一个图书管理项目。我有一个表来存储学生的详细信息。我希望每个用户的电话号码,电子邮件ID,用户名和密码都应该是唯一的。任何人都可以帮我如何为此编写存储过程,以便不再插入相同的数据。我写的查询粘贴在下面,但它不起作用。请帮我解决这个问题。





 声明  @icount   int  
set @ icount =(选择 count(*)来自 PatronDetails 其中​​(PhoneNo 喜欢 @ PhoneNo (电子邮件喜欢 @ Email (usernme 喜欢 @ usernme (pwd 喜欢 @ pwd ))
if @ icount = 0


insert into PatronDetails
(RegId,FirstName,Patron_Adrs,City,Zipcode,Country,Patron_State,Gender,
DOB,PhoneNo,Email,Course,CreatedDate,Isdeleted,FathersName,
YearOfJoining,Semester,SubCourse,usernme,pwd)

@ RegId @Firstname @ Patron_Adrs @ City @Zipcode ,<温泉n class =code-sdkkeyword> @ Country @ Patron_State
@ Gender @ DOB @ PhoneNo @ Email @ Course ,GETDATE(), 0 @FathersName
@ YearOfJoining @ Semester @ SubCourse @ usernme @ pwd





提前致谢

解决方案

您好,





你可以这样检查副本..



如果不存在(从PatronDetails中选择*,其中PhoneNo = @ PhoneNo和Email = @Email和usernme = @ usernme和pwd = @ pwd )

开始
//插入命令
结束





谢谢


Hello everyone,
I am doing a library management project.I have a table to store the student details.I want the phone number,email id,username and password of each user should be unique.Can any one help me how to write the stored procedure for this so that same data is not inserted again.The query i have written is pasted below but it is not working.Please help me resolving this.


declare @icount int
set @icount= (select count(*) from PatronDetails where (PhoneNo like @PhoneNo) and (Email like @Email) and (usernme like @usernme) and (pwd like @pwd))
if (@icount = 0)


    insert into PatronDetails
    (RegId,FirstName,Patron_Adrs,City,Zipcode,Country,Patron_State,Gender,
    DOB,PhoneNo,Email,Course,CreatedDate,Isdeleted,FathersName,
    YearOfJoining,Semester,SubCourse,usernme,pwd)
    values
    (@RegId,@Firstname,@Patron_Adrs,@City,@Zipcode,@Country,@Patron_State,
    @Gender,@DOB,@PhoneNo,@Email,@Course,GETDATE(),0,@FathersName,
    @YearOfJoining,@Semester,@SubCourse,@usernme,@pwd)



Thanks in advance

解决方案

Hello ,


you can check the duplicate like this way..

if not exists (select *  from PatronDetails where PhoneNo=@PhoneNo and Email =@Email and usernme=@usernme  and pwd=@pwd )

begin
    //insert command
end



thanks


这篇关于在将数据插入数据库时​​避免重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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