NSN
NSN

Reputation: 750

Argument assignment not working?

IF (@SP_ARGUMENT IS NULL )

SET @SP_ARGUMENT = (SELECT TOP(1) ID FROM TEST_TABLE )

SELECT @SP_ARGUMENT AS ARGUMENT_VAL

The above is code fragment of my stored proc.

But when I send sp_argument is null, I want it to use the 1st ID from the TEST_TABLE for the further queries below, but not working. What I am doing wrong here?

Pls help.

Upvotes: 0

Views: 58

Answers (2)

pingoo
pingoo

Reputation: 2064

You could use IsNull to do this:

Set @SP_ARGUMENT = IsNull(@SP_ARGUMENT, (SELECT TOP(1) ID FROM TEST_TABLE ))

SELECT @SP_ARGUMENT AS ARGUMENT_VAL 

Upvotes: 1

codingbadger
codingbadger

Reputation: 43974

In order to properly control the flow of your T-SQL commands you need to wrap the SET statement in a Begin/End statement.

IF (@SP_ARGUMENT IS NULL )
BEGIN
SET @SP_ARGUMENT = (SELECT TOP(1) ID FROM TEST_TABLE )
END
SELECT @SP_ARGUMENT AS ARGUMENT_VAL

Upvotes: 0

Related Questions