0

I need assign the following SQL Server's query result value to the variable called @value1

SELECT * 
FROM customer 
WHERE apo_id = '2589';

How can I do this in SQL Server?

9
  • Is $value1 a variable in your client programming language? Commented Oct 8, 2019 at 17:42
  • no it is my own variable name Commented Oct 8, 2019 at 17:45
  • Variables, in SQL Server, are prefixed with an @, not a $. If you're trying to put the results set into a variable in your application code the things you should be tagging are the application code language, and included your existing application code. Commented Oct 8, 2019 at 17:45
  • DECLARE @Var Datatype; SELECT @Var = Column FROM customer WHERE apo_id = '2589'; Note this won't work as expected if you have more than 1 row with the condition apo_id = '2589' Commented Oct 8, 2019 at 17:45
  • 1
    Considering the OP is using *, @Sami , I suspect they want to store all the columns (and rows?) into a single variable. Difficult to really know what they are after at the moment though. Commented Oct 8, 2019 at 17:46

2 Answers 2

2

1 - First declare your variable of type table.

declare @value1 table(
  --YOUR TABLE DEFINITION ex: ValueId int, 
)

2 - Insert into your variable

insert into @value1 select * from customer WHERE apo_id = '2589';

Hope that helps, thanks.

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

2 Comments

why you are declare table?
Since your return from query is a tabular return you need to declare a table to use as a return.
1

It won't really be a variable but a table because you are selecting multiple fields (e.g. Select *) but, you can select INTO a temporary table like this:

SELECT * 
INTO #myTempTable 
FROM customer 
WHERE apo_id = '2589';

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.