Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to store values in variable in SQL Server?

Author  Topic 

Shadab Shah
Starting Member

23 Posts

Posted - 2012-07-30 : 23:13:28
Hi All,
I am new to SQL Server.Prior to this i had done some programming in C.

Consider the below query,

select empid from Emp where deptid=2.

The output of the above query is

empid
1
3
4
5
10
14
15
18
19
20

now i want to store this values in variable and then used the variable in some future query. Since i cannot used array i would like to know what to used.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-07-30 : 23:30:48
You need to change your mindset when programming for database. You need to get familiar with working with "record set" rather than one value at a time as in C or other procedural language.

This is a good article to start
http://www.simple-talk.com/sql/database-administration/the-road-to-professional-database-development-set-based-thinking/


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Shadab Shah
Starting Member

23 Posts

Posted - 2012-07-31 : 00:09:09
Thanks khtan. It was a good article but did not resolve my query. I am still curious to know what can be done in the above scenario. I had tried using a variable but it stores only the last variable.I would apprciate your time if you try to explain.
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2012-07-31 : 01:59:39
What are you going to do with the data? If you're going to use it in another query then you should just incorporate the SQL into the other query.
One of the worst things you can do in RDBMS is take copies of data or somehow cache it. It just does not work in that way and you'll be fighting it the whole way.
Tell us what you're after and we can help. As an ex C programmer myself I understand your position but you need to get over it.
Go to Top of Page
   

- Advertisement -