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
 General SQL Server Forums
 New to SQL Server Programming
 While loop in SQL?

Author  Topic 

windex
Starting Member

3 Posts

Posted - 2011-12-13 : 10:52:17
So here's what I'm looking to do:

I have one table that is a list of names:
Tech, EmpID
ChrisD, 123456
JoeS, 123457
TedL, 123458

I'm creating a view that pulls data from a few different tables for one of the people above:
Select stuff from tables where Tech='ChrisD'

What I want to do is run the above Select statement for each Tech in the first table and Union all the results together.

I started looking at cursors to do this, but then found a heap of info saying not to do it. What's the best way to do this?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-13 : 10:59:52
isnt it just of matter of joining them?

SELECT t.Tech, t.EmpID,t1.Stuff,..
FROM FirstTable t
INNER JOIN tables t1
ON t1.Tech = t.Tech


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sunnyleoneo
Starting Member

19 Posts

Posted - 2011-12-13 : 12:35:23
Visakh16 is correct.
No need of cursor, actually you should avoid cursor as much as possible.

SELECT stuff
FROM tablesname
INNER JOIN tablewithtech
ON tablesname.Tech = tablewithtech.Tech
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2011-12-13 : 13:41:49
Don't forget to mark sunny's answer as accepted

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-12-13 : 14:31:13
ummm....they said UNION

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

windex
Starting Member

3 Posts

Posted - 2011-12-14 : 15:15:22
Someone here showed me what I needed to do. I just needed to change my select statement to:

Select stuff from tables where Tech IN (Select Techs From TechTable)

I was thinking that each of the special calculations in 'stuff' was going to calculate differently than it did.
Go to Top of Page

windex
Starting Member

3 Posts

Posted - 2011-12-14 : 15:16:30
Thanks for the replies either way. How do I mark someone's answer as accepted?
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-12-14 : 17:15:03
SQLTeam doesn't mark answers that way. You can post a in thanks though.
Go to Top of Page
   

- Advertisement -