I already have a stored procedure GET_ROW
that is doing a select, according to input ID:
SELECT TOP 1 *
FROM MyTable
WHERE ID = @ID
Now, I want to create another stored procedure that checks if an @ID
exists. If it exists, return the existing row. Otherwise, create a new row with the requested @ID
and return it.
So, I'm thinking of something like this:
Declare ResRow
begin tran
ResRow = Exec GET_ROW @ID
if exists (ResRow)
return ResRow
else
Insert into ...
return Exec GET_ROW @ID
commit
After executing this code, I want to be sure that only one row with @ID
exists in the database (no duplicated row with same ID)