Solved

MS SQL 2000 - How to reuse a subquery?

Posted on 2006-11-22
5
785 Views
Last Modified: 2008-02-26
I have a long query, with some sub-queries. The sub-query (or derived table) is repeated. I had to use UNION to put the results together. The query works but it's too long.

select ....
from
(select  ......, ......, ......, ......, ......  
  from
 Mytable)
as Mysubquery
.......
union
select ....
from
(select  ......, ......, ......, ......, ......  
  from
 Mytable)
as Mysubquery
.......
.......
The sub-query "Mysubquery" above is exactly the same, and is repeated in multiple places in the sql query. This makes the code very lengthy (because the sub-query is kind of lengthy). What is the best way to specify the sub-query in one place and re-use through out the code? I guess I can use a view, but there must be a better (more efficient way), maybe through using temp tables or stored procedures?
0
Comment
Question by:novice12
[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
  • 2
5 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 500 total points
ID: 17998223
A view if this is really commonly used throughout your application, a temp table (or table variable) if it is only specific to one stored procedure or select statement
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17998756
I would create a temp table with the results of the subquery is not correlated.
if you have sql server 2005, you might use a function and apply the "cross apply" syntax with that.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17998759
now, possibly you can rewrite your union?
0
 

Author Comment

by:novice12
ID: 18020699
What is "cross-apply"?. I have SQL server 2000.
0
 
LVL 29

Expert Comment

by:Nightman
ID: 18020879
It's specific to SQL 2005.
have a look here: http://www.databasejournal.com/features/mssql/article.php/3616286

Again, the recommendations are for you to use a temp table or table variable
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

707 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