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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query Impossible?
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

13 Posts

Posted - 06/23/2011 :  16:46:14  Show Profile  Reply with Quote
Hello all,

I have a tough question that I hope can be fully explained and easily understood through text with little/no visual help.

I have 5 queries that I run in a sequential order to get 5 different answers.

Query #1 uses a 'Identifying Code' to pull an 'Operating Number' and 'Phone Number'

Query #2 uses the 'Operating Number' from #1 to find an 'Operating Name'

Query #3 uses 'Identifying Code' to pull 'Server_Name'

Etc Etc....

But my problem (and reason I dont use 'Inner Join/Where' is that Query 2-5 use a single result from Query 1.

Any help would be greatly appreciated. I understand im being very vague with this question. Here is a post of my multi-stepped query:

from dbo.DATA (nolock)
where place like ('Texas') and BLOCK_ID='A'
order by place

select *
from dbo.DATA_NAME
where call_number in ('1234')

select *
from dbo.DATA_CallNAme
where place in ('Texas')

select *
from city
where city = 'Dallas'
and Status = 'Active'

from phone_list
where phone_first='906' and phone_last='676'

It is important to note that phone_first, phone_last, city, and call_number are generated by #1.

Is it possible to have the remaining queries auto-populate with these answers??

Almighty SQL Goddess

38200 Posts

Posted - 06/23/2011 :  16:53:29  Show Profile  Visit tkizer's Homepage  Reply with Quote
Using #1, put them all into variables and then use those variables for your further queries.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server

Subscribe to my blog
Go to Top of Page

Yak Posting Veteran

55 Posts

Posted - 06/24/2011 :  06:19:19  Show Profile  Reply with Quote
something like:

IF #1 returns ONLY one result

declare all variables...

select @phone_first = phone_first, @phone_last = phone_last, @place = place, @call_number = call_number
from dbo.DATA (nolock)
where place like ('Texas') and BLOCK_ID='A'
order by place

IF #2 could possible return more than one result

INSERT all results from #1 into a temp table

following.. in #2 - #5.. WHERE xxxx IN () etc.. should work

Where software development knowledge meets the reader
Go to Top of Page

Starting Member

13 Posts

Posted - 06/24/2011 :  10:15:23  Show Profile  Reply with Quote
Thank you very much tkizer and jfarrugia!
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000