Wrox Programmer Forums
|
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
 
Old March 10th, 2008, 01:32 PM
Authorized User
 
Join Date: Mar 2007
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!

 
Old March 10th, 2008, 03:57 PM
Friend of Wrox
 
Join Date: Jun 2003
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
[email protected]
 
Old March 11th, 2008, 09:58 AM
Authorized User
 
Join Date: Mar 2007
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!!!








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. [email protected] 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





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