Solved

How do I Query the Result Set of a Query in MS SQL 2000

Posted on 2007-11-22
4
204 Views
Last Modified: 2010-04-21
I'd like some advice on how to create a query.

In my stored procedure, I've got several variables which are used to form a query.  The query is created as a string and then executed:

CREATE PROCEDURE [dbo].[prcGetUCC]
-- Declare input paramters
@SortExp VARCHAR(100) = NULL,
@Branch VARCHAR(20) = NULL,
@BranchJ VARCHAR(20) = NULL

IF @Branch ='All'
    BEGIN
    SET @SQLStatement='SELECT * FROM vUCC ORDER BY ' + @SortExp + ' ASC'
    END
ELSE
    BEGIN
    SET @SQLStatement='SELECT * FROM vUCC WHERE BoO = ''' + @BranchJ + ''' OR
    BoO = ''' + @Branch1 + ''' OR BoO = ''' + @Branch9 + ''' ORDER BY ' + @SortExp + ' ASC'
    END
EXEC (@SQLStatement)

What I want to do is then query the results of this query and further filter the data.  Something like this:

SELECT * FROM (The Result Set of the Previous Query) WHERE ColumnA = ValueB

Is this at all possible?
0
Comment
Question by:Joseph Hornsey
  • 2
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 400 total points
ID: 20337585
yes, but you have to include it in the dynamic sql altogether:

CREATE PROCEDURE [dbo].[prcGetUCC]
-- Declare input paramters
@SortExp VARCHAR(100) = NULL,
@Branch VARCHAR(20) = NULL,
@BranchJ VARCHAR(20) = NULL 
AS
SET NOCOUNT ON
IF @Branch ='All'
    BEGIN
    SET @SQLStatement='SELECT * FROM vUCC ORDER BY ' + @SortExp + ' ASC'
    END
ELSE
    BEGIN
    SET @SQLStatement='SELECT * FROM vUCC WHERE BoO = ''' + @BranchJ + ''' OR
    BoO = ''' + @Branch1 + ''' OR BoO = ''' + @Branch9 + ''' ORDER BY ' + @SortExp + ' ASC'
    END 
SET @sqlstatement = 'SELECT * FROM (' + @sqlstatement + ') l WHERE ColumnA = ValueB ' 
EXEC (@SQLStatement)

Open in new window

0
 
LVL 14

Author Comment

by:Joseph Hornsey
ID: 20337597
No way.  Really?

So, you can really SELECT Value FROM (SELECT DiffValue FROM DiffTable) WHERE This = That?

I'll try this in the morning and will post back.  Unfortunately, I'm beat.

Thanks!

<-=+=->
0
 
LVL 25

Assisted Solution

by:imitchie
imitchie earned 100 total points
ID: 20337665
SELECT * FROM (The Result Set of the Previous Query) ALIAS WHERE ColumnA = ValueB

As long as you give it an alias. Angel's sample names it as L (lowercase). It can be used just like a normal "table", so you can even reference fields in it.
Take care that if you alias the columns, then you reference by alias, not original name. i.e.

SELECT * FROM ( SELECT Cost * 1.4 as SalePrice, ProductName FROM PRODUCTS) ProdAndSalePrice WHERE SalePrice > 100
0
 
LVL 14

Author Closing Comment

by:Joseph Hornsey
ID: 31410632
I've split the points because I didn't notice the lower-case L in angel's example.  If imitchie hadn't clarified that, I would have totally missed it.

Thank you both so much for your help!

By the way, is this an example of a "derived table"?

<-=+=->
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question