mogasiru
mogasiru

Reputation: 1

How to assign SQL query result to variable?

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?

Upvotes: 0

Views: 9499

Answers (2)

Guilherme Martin
Guilherme Martin

Reputation: 847

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.

Upvotes: 2

beeker
beeker

Reputation: 810

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';

Upvotes: 1

Related Questions