is it possible to use one query to another query input

Hi ,

i want to select one product and i wan to pass that product to another query . is that possible ??

1 query return multiple value and the have join 3 tables , Its return the product

i wan to use the product in second query is that possible ??
LVL 20
Sathish David  Kumar NArchitectAsked:
Who is Participating?
Scott PletcherSenior DBACommented:
Yes.  The common way to do that is to use a derived table, which is a subquery within the main query.

For example:

SELECT x..., derived...
FROM tablex x
    SELECT b.product, ...
    FROM tablea a
    INNER JOIN tableb b ON ...
    INNER JOIN tablec c ON ...
    WHERE ...
) AS derived ON
    derived.product = x.product
for Oracle or SQL Server?

do you have any more details? an existing query perhaps?
Gerwin Jansen, EE MVETopic Advisor Commented:
Select * from table2 where in (select id from table1);
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Alexander Eßer [Alex140181]Software DeveloperCommented:
with sel as
 (select id
    from table1)
select *
  from table2,
 where =;

Open in new window

Vitor MontalvãoMSSQL Senior EngineerCommented:
Yes, it's possible.
For a better support please post the queries and a sample of data and expected result.
Hi dravidnsr,

as already posted, with more info you can get better support, however here are some possible leads that may get you started on finding your own solution (assuming the two queries are in separate stored procs):

Use a table variable to store the result in query 1and pass that table variable to query 2 as a parameter
Use a (global or local) temporary table to store the result in query 1, then select from the temp table in query 2 (dont forget to clean up the temp table
Merge the two stored procedures into one
Define a view for the joins in query 1, make a list of IDs for the resulting records in query one and store them in a comma separated string. Pass this list to query two, parse the IDs out of the string and select records from the view based on the list of IDs.

Kind regards,

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.