Friday, 13 February 2015

Insert Unique Value in MS SQL Database By Using SQL Stored Procedure

When we enter some data in database then its repetition is major problem for at all. Here I am using a table with Mobile No. as primary key and trying to insert unique value to it.

You can see database in this image.
 
Stored procedure wants phone no as a parameter which can be passed easily

create procedure insert_unique
(
@phone numeric(10,0)
)
as
begin
INSERT INTO
PhoneData(Phoneno)
    SELECT @phone
WHERE NOT EXISTS (SELECT phoneno FROM PhoneData WHERE Phoneno = @phone)
end

Pass your phone no in this procedure and it will insert only unique record.