How to check the input data is already exist or not in the database?
Thank you for your help.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sibeesh VenuRank #27 · 42.9K reputationPosted Apr 26, 2015, 9:47 PM EDT
You can follow so many ways to achieve this. Some of them are explained below.
Please have a look.
http://forums.asp.net/t/1345572.aspx?C+Sql+server+checking+whether+a+value+already+exists+in+the+Database
http://www.codeproject.com/Questions/565011/StoredplusProcedureplusC-plus-plus-IFplusEXIST
https://support.office.com/en-au/article/Create-and-use-an-index-to-improve-performance-0a8e2aa6-735c-4c3a-9dda-38c6c4f1a0ce?CorrelationId=60f214c1-5c47-458a-8b62-5ab2eebc5c90&ui=en-US&rs=en-AU&ad=AU
Please up vote and accept if it helps you.
Kindest Regards
Sibeesh Venu
www.sibeeshpassion.com
Asp.Net HeinRank #6,010 · 279 reputationPosted Apr 26, 2015, 9:49 PM EDT
Syed ShanuRank #87 · 23.7K reputationPosted Apr 26, 2015, 9:56 PM EDT
sample table creation
IF EXISTS ( SELECT [name] FROM sys.tables WHERE [name] = 'UserMasters' )
DROP TABLE UserMasters
GO
CREATE TABLE [dbo].[UserMasters](
[USR_ID] [varchar](20) NOT NULL,
[User_Name] [varchar](100) NOT NULL,
[Email] [varchar](100) NOT NULL,
[Phone] [varchar](20) NOT NULL,
[Address] [varchar](300) NOT NULL
CONSTRAINT [PK_UserMasters] PRIMARY KEY CLUSTERED
(
[USR_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
INSERT INTO [DynamicProject].[dbo].[UserMasters]
([USR_ID],[User_Name],[Email],[Phone],[Address])
VALUES
('USR001','SAHNU','[email protected]','01030550000','From India Lives In Korea')
// here is a sample stored procedure where i will check for Userid and userName is already exisit or not.If its not exisit then i will insert the data.
-- Author : Shanu
-- Create date : 2015-02-05
-- Description : To Insert User Master
-- Tables used : ItemMasters
-- Modifier : Shanu
-- Modify date : 2015-02-05
-- =============================================
-- exec USP_User_Insert 'Usr002','Afraz' ,'[email protected]','0100000','my address'
-- =============================================
Create PROCEDURE [dbo].[USP_User_Insert]
(
@USR_ID VARCHAR(20) = '',
@User_Name VARCHAR(100) = '',
@Email VARCHAR(100) = '',
@Phone VARCHAR(100) = '',
@Address VARCHAR(100) = ''
)
AS
BEGIN
IF NOT EXISTS (SELECT * FROM UserMasters WHERE User_Name=@User_Name)
BEGIN
INSERT INTO [UserMasters]
([USR_ID],[User_Name],[Email],[Phone],[Address])
VALUES
(@USR_ID,@User_Name,@Email,@Phone,@Address)
select 'Inserted' as 'Result'
END
ELSE
BEGIN
select 'Error Insert' as 'Result'
END
END
-- To execute and test the SP run the below code.
exec USP_User_Insert 'Usr002','Afraz' ,'[email protected]','0100000','my address'
Akhil GargRank #772 · 3K reputationPosted Apr 26, 2015, 9:45 PM EDT