Solved

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

Posted on 2007-11-22
4
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
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 …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 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