Solved

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

Posted on 2007-11-22
4
195 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 142

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 13

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 13

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now