I am looking for a more efficient way to accomplish this task. I need to set a variable equal to ID if it exists, and if not insert it, and then set the variable in the inserted identification information. I can accomplish this by doing the following:
@VariableName --sent through to stored procedure DECLARE @VariableID [int] IF EXISTS(SELECT VariableID FROM VariableTable WHERE VariableName = @VariableName) SET @VariableID = (SELECT VariableID FROM VariableTable WHERE VariableName = @VariableName) ELSE INSERT INTO VariableTable(VariableName) VALUES(@VariableName) SET @VariableID = SCOPE_IDENTITY(); END
However, it seems that it is inefficient to run the same query twice (check if it exists and if it sets the variable)
Just look for suggestions on how best to complete this task.
jon3laze
source share