Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 10th, 2008, 01:32 PM
Authorized User
Points: 254, Level: 5
Points: 254, Level: 5 Points: 254, Level: 5 Points: 254, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2007
Location: , , .
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default Query a Query?

I know I can query an open query, but is it possible to query a query?

As an example, in an open query, I can do the below and it gives me a count.

select count(*) from openquery(D2SQL01,'

SELECT
      FNAME,
      LNAME,
      ADDRESS,
      CITY,
      STATE,
      ZIP
FROM TBL_ADDRESS
')

Thanks!

Reply With Quote
  #2 (permalink)  
Old March 10th, 2008, 03:57 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Sure.

A query is a derived table, so it can be used anywhere where a table can be used. This is usually in a FROM clause or subquery. The only 'trick' is that you must give a name to the derived table (the query). This is so its columns can be unambiguously referred to if needed:

Code:
SELECT COUNT (*)
  FROM (SELECT
          FNAME,
          LNAME,
          ADDRESS,
          CITY,
          STATE,
          ZIP
        FROM TBL_ADDRESS) as DerivedTable
Jeff Mason
je.mason@comcast.net
Reply With Quote
  #3 (permalink)  
Old March 11th, 2008, 09:58 AM
Authorized User
Points: 254, Level: 5
Points: 254, Level: 5 Points: 254, Level: 5 Points: 254, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2007
Location: , , .
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Jeff, you gave me the exact answer I needed!! I didn't realize the query needed to be named. That's why I couldn't get it to work!!!



Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Output Query to txt file from SQL Query everest SQL Server 2005 4 November 22nd, 2007 01:49 AM
how to make a query from an existing query raport SQL Language 3 November 13th, 2006 08:59 PM
I solved insert query.now see this Update Query. amit_mande@yahoo.com VB.NET 2002/2003 Basics 2 September 21st, 2006 12:48 AM
Syntax error in query. Incomplete query clause. dispickle ADO.NET 3 April 16th, 2004 01:04 PM
Error on Make-Table Query In Union Query rylemer Access 1 August 20th, 2003 07:42 PM



All times are GMT -4. The time now is 05:09 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.