I need to write a query that receives a set of information from a table, but if there is no information for this particular client, use the default set. I was wondering if there is a way to avoid duplicate requests in the if (exists ()) statement.
For example:
IF( EXISTS( SELECT * FROM UserTable WHERE Name = @UserName))
BEGIN
SELECT * FROM UserTable WHERE Name = @UserName))
END
ELSE
BEGIN
SELECT * FROM UserTable WHERE Name = 'Jon Skeet'))
END
The first two choices (the true part of the if exists) are accurate. I want to avoid executing the same query twice if the statement is true. I know there is a stop when the first true condition is met, but this is still O (n) the worst case.
Another option that I know of is putting information in a temporary table and checking if there is information if it does not return the default information.
, ? ? WHERE, , ?
: . , temp, . , , .