Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sql 2000 - can I use union with stored procedures?

Posted on 2006-11-28
6
Medium Priority
?
3,903 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
[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
6 Comments
 
LVL 9

Expert Comment

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

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
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.

 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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

722 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