Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MS SQL 2000 - How to reuse a subquery?

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

598 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