4
\$\begingroup\$

I want to return the title of a record if one exists or the word 'invalid' if not:

    @ID int ,
    @Title nvarchar(50) output
    ...
if exists(select * from MyTable where ID=@ID)
    select @Title = (select Title from MyTable where ID=@ID);
else select @Title = 'invalid';

but I don't have a good feeling with this query.

\$\endgroup\$
5
\$\begingroup\$

Just do:

// Default value
select @Title = "invalid";

// If a record is found, replace "invalid" by the Title value
select @Title = Title from MyTable where ID=@ID;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.