Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-11-22
4
Medium Priority
?
213 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 1600 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 400 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

715 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