SQL Insert With Unique Constraint using Stored Procedure
This is a generic question with a specific example.
I have a table with three fields (genreID (PK IDENTITY), genre, and
subGenre). The table has a unique constraint on (genre, subGenre)
combination.
I am wondering how I could go about modifying the stored procedure to
insert if it DOES NOT exist in the table, otherwise return the genreID of
the existing genre if it DOES exist.
CREATE PROCEDURE spInsertGenre
@genreID int OUTPUT,
@genre varchar(100),
@subGenre varchar(100)= NULL
AS
BEGIN
INSERT INTO Genre
(
genre,
subGenre
)
Values (
@genre,
@subGenre
)
SELECT @genreID = SCOPE_IDENTITY()
END
GO
No comments:
Post a Comment