Solved

MS SQL 2000 - How to reuse a subquery?

Posted on 2006-11-22
5
780 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
  • 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 142

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 142

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

809 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