Assigning a variable in T-SQL

At the end of my function, I have a statement:

RETURN @Result

      

I want to do something like this:

IF (@Result = '')
BEGIN
@Result = 'Unknown'
END

RETURN @Result

      

The above doesn't work.

0


a source to share


6 answers


SET @Result = 'Unknown'

      



;)

+4


a source


IF (@Result = '')
BEGIN
    SELECT @Result = 'Unknown'
END

RETURN @Result

      



Note that the assignment method in T-SQL is operator SELECT

. You can also use the operator SET

, although this is not recommended.

+2


a source


change this line

@Result = 'Unknown'

      

to

set @Result = 'Unknown'

      

+2


a source


I think you need to check if @result is NULL because NULL is not the same as ''

IF (ISNULL(@Result, '') = '')
BEGIN
    SET @Result = 'Unknown'
END

RETURN @Result

      

+1


a source


IF (@Result = '')

TO BEGIN

SET @Result = 'Unknown'

      

END

RETURN @Result

0


a source


SET @Result = 'Unknown'

      

@Justice: According to Microsoft - MSDN SELECT @Result = 'Unknown'

is not recommended at all

0


a source







All Articles