Solved

sql 2000 - can I use union with stored procedures?

Posted on 2006-11-28
6
3,894 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
Comment Utility
you cannot do that
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
How can I transform a select query into a function?

create function1()
as
 begin
  select * from table1
 end

Is the above possible?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
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
Comment Utility
I will go with the view. It's much easier. Thanks
0
 

Expert Comment

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

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 …
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

772 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

12 Experts available now in Live!

Get 1:1 Help Now