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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Syed ShanuPosted Apr 27, 2015, 1:56 AM
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'
Asp.Net HeinPosted Apr 27, 2015, 1:49 AM
Sibeesh VenuPosted Apr 27, 2015, 1:47 AM
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
Akhil GargPosted Apr 27, 2015, 1:45 AM