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
 Query Impossible?

Author  Topic 

yogi86
Starting Member

13 Posts

Posted - 2011-06-23 : 16:46:14
Hello all,

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

Problem:
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:

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

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

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

--b.
select *
from city
where city = 'Dallas'
and Status = 'Active'


--4.)
select*
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??

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-06-23 : 16:53:29
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
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jfarrugia
Yak Posting Veteran

55 Posts

Posted - 2011-06-24 : 06:19:19
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

yogi86
Starting Member

13 Posts

Posted - 2011-06-24 : 10:15:23
Thank you very much tkizer and jfarrugia!
Go to Top of Page
   

- Advertisement -