Solved

sql 2000 - can I use union with stored procedures?

Posted on 2006-11-28
6
3,896 Views
Last Modified: 2008-01-09
I am using SQL 2000
I have a very lenght sql query with many select queries joined with UNION, like this:
---------------------------------------------------------------------------------------------------
select
Col1, Col2, COl3, Col4
form MyTable1
uion
select
Col1, null, Col2, Col3
from MyTable2
...
....
-----------------------------------------------------------------------------
If I create stored procedures as follows:

Create procedures as sp1
select
Col1, Col2, COl3
form MyTable1

create procedures as sp2
select
Col1, Col2, COl3
form MyTable2

-----------------------------------------------------------------------------
I would then like to do something like this
select * from
(exec sp1) as T1
union
select * from
(exec sp2) as T2
-------------------------------------------------------------------------------
I tried the above and It doesn't work (syntax errors): Incorrect syntax near the keyword 'execute'.
Is it possible to use UNION when executing stored procedures?

0
Comment
Question by:novice12
6 Comments
 
LVL 9

Expert Comment

by:gpompe
ID: 18031146
you cannot do that
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18031150
no, you cannot use UNION with stored procedures.

but you CAN use UNION with stored functions, so if you can transform your procedures into functions, you can do this:

select * from dbo.Function1()
union all
select * from dbo.Function2()


note that you should use UNION ALL wherever possible, as UNION alone does a implicit DISTINCT on the returned rows, which first of all costs resources and is often giving confusion
0
 

Author Comment

by:novice12
ID: 18031264
How can I transform a select query into a function?

create function1()
as
 begin
  select * from table1
 end

Is the above possible?
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 18031284
now, if you are only doing a select from a table without any parameters, you will even better be off to use a view instead of procedures/functions.

create view view_1
as
 begin
  select * from table1
 end


now, the function way would be:


create function function1()
returns table ( col1 int, col2 varchar(20)  )
as
 begin
  return ( select field1, field2 from table1)
 end


with a paramter:

create function function1( @param int )
returns table ( col1 int, col2 varchar(20)  )
as
 begin
  return ( select field1, field2 from table1 where somefield = @param)
 end


0
 

Author Comment

by:novice12
ID: 18031329
I will go with the view. It's much easier. Thanks
0
 

Expert Comment

by:laparico2002
ID: 20470706
I have a stored procedure as written below;
CREATE PROCEDURE StaffnFamily
AS
SELECT EmployeeId, Name, DateOfBirth, RelationShip
FROM FamilyDetails union
 ( SELECT EmployeeId, Name, DateOfBirth, 'Self' AS "_'Self'_"
FROM Employees )
GO

But when i called it in my vb 60 program, it give me an error; "Invalid object name 'StaffnFamily'.
can this stored procedure be converted in view for me to use? if so how?
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

825 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