0

I have a SQL Server table #SqlData with the following column splitdata.

splitdata
---------
BB10_1_X
4759
566549

I want to store these 3 column values into 3 different variables. Please help me as I am a beginner and I don't know how to do it..

3
  • 1
    This question is not clear. What do you mean by variable? Is your table currently in SQL? If so, what are the columns? If not, in what format are your data currently stored? What all have you tried so far? Commented Apr 15, 2014 at 18:35
  • i have modified the question. I hope it will help. thnakns Commented Apr 15, 2014 at 18:39
  • This appears as to be a follow-up to stackoverflow.com/questions/23088022/…, but I'm not sure its the right idea. That is, you'l only get this sort of arbitrary #SqlData temp table if you are are splitting your source into rows. Commented Apr 15, 2014 at 18:55

1 Answer 1

2

You can declare variables like this:

DECLARE @Var AS VARCHAR(MAX) 

And you can assign them using a select statement like this:

SELECT @Var=MyColumn 
FROM MyTable 
WHERE <My Condition>

There are plenty of resources on TSQL if you google it.

In your situation, if you're sure you will have exactly three rows, you can use three separate select statements:

DECLARE @Var1 AS VARCHAR(MAX), @Var2 AS VARCHAR(MAX), @Var3 AS VARCHAR(MAX)

SELECT @Var1=MyColumn 
FROM MyTable 
WHERE <My Condition That Returns First Row>

SELECT @Var2=MyColumn 
FROM MyTable 
WHERE <My Condition That Returns Second Row>

SELECT @Var3=MyColumn 
FROM MyTable 
WHERE <My Condition That Returns Third Row>
Sign up to request clarification or add additional context in comments.

Comments

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.