July 30, 2012 at 9:50 pm
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.
July 30, 2012 at 9:59 pm
Probably you can assign this resultset to a table variable or a temp table and can make use of the same later when required.
July 30, 2012 at 10:15 pm
That's a valid point. But what if my query is something like this
--------- some logic
--------- some logic
where empid=(select empid from #emp where deptid=2)
The objective of the query is to execute the logic for each empid.
July 30, 2012 at 10:23 pm
--------- some logic
--------- some logic
where empid=(select empid from #emp where deptid=2)
In this case as i said you can insert it (select empid from #emp where deptid=2) to an temptable so tat this temp table 'll be having the corresponding data only. so where ever required you can join with this temp table and achieve the result.
select empid into #TempEmp from emp where deptid=2
--------- some logic
--------- some logic
from tablename TN join #TempEmp TE TN.empid=TE.empid
Hope this Works......
July 30, 2012 at 10:37 pm
Not all personnel are employee's, Joe. Nor is it always wise to combine the two.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 30, 2012 at 10:40 pm
shahsn11 (7/30/2012)
That's a valid point. But what if my query is something like this--------- some logic
--------- some logic
where empid=(select empid from #emp where deptid=2)
The objective of the query is to execute the logic for each empid.
You can execute the logic for sets of employees in the department instead of for each empid.
--Jeff Moden
Change is inevitable... Change for the better is not.
July 30, 2012 at 10:42 pm
Ok that answers my question. Thank you very .... much 🙂 .
But as i was going through your question one more question pop up in my mind.
This code is not the exact code but little bit like a pseudo code
Insert all the records in Emp2 from Emp where deptid=2
which should look something like this
update Emp2 set empid= empid from Emp where deptid=2
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply