?
Solved

MS SQL 2000 - How to reuse a subquery?

Posted on 2006-11-22
5
Medium Priority
?
787 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

762 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