Solved

MS SQL 2000 - How to reuse a subquery?

Posted on 2006-11-22
5
777 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
Comment Utility
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]
Comment Utility
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]
Comment Utility
now, possibly you can rewrite your union?
0
 

Author Comment

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

Expert Comment

by:Nightman
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how the fundamental information of how to create a table.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now