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 2000 Forums
 SQL Server Development (2000)
 how to use one sql query in another sql query

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-11-12 : 08:53:11
Sreedhar writes "hi!!
i am porting an application from access to the web. i am using asp and ms sql.
in access, i have run into a situation, where the programmer has constructed one query and has used that query in another query.

for example,the programmer has constructed a query called "Q01". later, in another query, he uses values such as "Q01.fields("a")"

the solution i came up with is that i create a temporary table containing the values of the first query ("Q01"). and then when i am constructing the second query, i use the values from the temporary tables. but, as the queries become more and more complicated, using "transform" and "pivot", i get errors, the most common being "syntax error at "FROM" clause".

is there any solution wherein i can directly use the first query in the second one, like it was done in access?

i'd greatly appreciate your help on this."

JustinBigelow
SQL Gigolo

1157 Posts

Posted - 2002-11-12 : 09:03:58
You can use the first query as a derived table. For example...


select city, zip
from
(select city, state, zip
from table1
where country like 'USA') t
where state like 'TX'


It's also common to find a nested query in the IN statement of your WHERE clause.

hth,
Justin



Have you hugged your SQL Server today?

Edited by - justinbigelow on 11/12/2002 09:04:14
Go to Top of Page

r937
Posting Yak Master

112 Posts

Posted - 2002-11-12 : 09:12:47
or declare the first query as a view

then the second query will not need alteration

this is best when several second queries use the same first query


rudy
Go to Top of Page
   

- Advertisement -