1

Can you assign more than one variable under a single condition in a select statement, like in Visual Basic:

If x=1 then
   y=2
   z=5
End If

I have lots of complicated conditions that I don't want to keep repeating, over and over.

I have SQL Server 2012.

1 Answer 1

1

Sure, here's how you would do it in t-sql, which I assume is what you're using:

DECLARE @y INTEGER
DECLARE @z INTEGER

IF @x = 1
BEGIN
  SET @y=2
  SET @z=5
END

Here's a SQL fiddle to see the above code in action.

Sign up to request clarification or add additional context in comments.

6 Comments

Well, I was really trying to do this in a select statement, and by condition was not an integer test, but a lot of complicated logic.
You could always make functions for the "complicated logic" so you don't have to repeat the logic over and over again in your select statement.
I thought functions only returned scalars or tables?
The idea is that if you have a complicated piece of logic, in which the final result is a single value, you can factor that logic into a function, and call that function to get the value. Anyway, maybe you can add more detail to your question and ask more specifics, it will be easier to know what you need.
My error was in the use of the term 'variable'. I meant colums.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.