TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
eliza sahoo
NA
41
0
SQL Server: Restrict the duplicate insertion of records to the database using Store Procedure
Apr 15 2010 3:10 AM
In SQL Server Database we can check the record before insert to the database and insert the record to the database by creating a store procedure as follows:
CREATE
PROCEDURE
usp_AddTestData_UsingExists
(
@Name
varchar
(
50
),
@Qualification
varchar
(
50
)
)
AS
DECLARE
@Result
int
BEGIN
TRANSACTION
IF
EXISTS
(
SELECT
NULL
FROM
TestData
WITH
(
UPDLOCK
)
WHERE
[Name]
=
@Name
AND
Qualification
=
@Qualification
)
BEGIN
SELECT
@Result
=
-
1
END
ELSE
BEGIN
INSERT
INTO
TestData
(
[Name]
,
Qualification
)
VALUES
(
@Name
,
@Qualification
)
SELECT
@Result
=
@@ERROR
END
IF
@Result
<>
0
BEGIN
ROLLBACK
END
ELSE
BEGIN
COMMIT
END
RETURN
@
Result
Reply
Answers (
0
)
how to write stored procedure to insert values into two tables?
Doubt in Sql Query