我插入时如何检查数据库中是否已经存在的数据 [英] how to check data it is already exists or not in database when i am inserting
本文介绍了我插入时如何检查数据库中是否已经存在的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我制作了一个Web应用程序,当我从文本框中向表中插入数据时,我想检查数据是否已存在.如果数据已存在,则我希望消息数据已存在"是什么类型我应该写的查询内容?
或如何使用不存在的查询?
我正在使用sql server.
在此先感谢
Hi, i made a web application and when i insert a data from text box to table i want to check data is already exist or not .if data is already exist then i want a message "data is already exists" what type of query i should wrote ?
or how to use not exists query ?
i am using sql server.
Thanks in advance
推荐答案
创建类似于以下&检查返回值并相应显示消息..
Create proc similar to the following & check the return value and show the message accordingly..
Create Procedure Proc_Name
(
@EMailAdress varchar(50) = null,
}
AS
IF EXISTS (SELECT UserID FROM E-MAIL WHERE EMailAdress = @EMailAdress)
BEGIN
return 0
END
ELSE
BEGIN
INSERT INTO E-MAIL (UserID,EMailAdress,Description) VALUES (UserID,@EMailAdress,@Description)
return 1
END
在代码中,检查返回值,例如...
In the code, check for the return value something like...
int returnValue = (int) comm.ExecuteScalar();
这篇关于我插入时如何检查数据库中是否已经存在的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文