I am having trouble with a stored proc (SQL 2005).
I have a table called tbrm_Tags with two columns, TagID and TagName. I want to pass a TagName value to the stored proc and then I want to :
1) Check if the Tagname exists and if it does return the TagID
2) If the Tagname does not exist I want it to insert into the table and return the TagID.
Here is the stored proc I am using:
@TagID int = null,
@TagName varchar(50)
AS
DECLARE @returnValue int
BEGIN
IF EXISTS (SELECT * FROM tbrm_Tags WHERE TagName = @TagName)
BEGIN
SELECT
TagID
FROM tbrm_Tags
WHERE TagName = @TagName
END
ELSE
BEGIN
IF NOT EXISTS (SELECT * FROM tbrm_Tags WHERE TagName = @TagName)
INSERT INTO tbrm_Tags
(
TagName
)
VALUES
(
@TagName
)
SELECT @returnValue = @@IDENTITY
END
END
RETURN @returnValue
I cannot get the select statement to return the TagID when the Tagname exists. Any help would be appreciated.