Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Simple SQL Union Problem

Posted on 2003-12-11
7
Medium Priority
?
1,127 Views
Last Modified: 2007-12-19
Hi, I am new at SQL.  What is wrong with my syntax?

Select *
From "STOREDPROCEDURE1"
UNION
Select *
From "STOREDPROCEDURE2"
ORDER BY ChangeNoNOTVAL;
0
Comment
Question by:eawucker
7 Comments
 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 9925195
Why can you not modify the stored proc so that union operation is performed there.


Try this. But result sets from proc1 and proc 2 should return a resultset so that the datatypes match.

create table #temp (
col1 int,
col2 int,
etc...
)

insert #temp
exec STOREDPROCEDURE1

insert #temp
exec STOREDPROCEDURE1

select *from #temp
0
 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 9925199
Cannot select from Stored Procedure.

create table #temp (
col1 int,
col2 int,
etc...
)

insert #temp
exec STOREDPROCEDURE1

insert #temp
exec STOREDPROCEDURE2

select *from #temp

 
 
 
0
 

Author Comment

by:eawucker
ID: 9925212
I think I explained wrong.  I want to create a stored procedure that unions 2 other stored procedures.  I can't get the syntax right.  What is it?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:curlypinhead
ID: 9925249
Why not create a new Stored Procedure and just union the queries in the 2 other stored procedures?

Example:

Sproc1:
select * from table1

Sproc2:
select * from table2

Sproc3 (new sproc):
select * from table1
union
select * from table2


I've never seen a union of stored procedures.  That doesn't mean it is impossible though.

curlypinhead
0
 
LVL 15

Expert Comment

by:namasi_navaretnam
ID: 9925426
To exeute both procs in one proc do the following:

Create Procedure sp_Main
As
Begin
create table #temp (
col1 int,
col2 int,
etc...
)

insert #temp
exec STOREDPROCEDURE1

insert #temp
exec STOREDPROCEDURE2

select *from #temp -- returns single result set

End

If you need 2 result sets then try:

Create Procedure sp_Main
As
Begin

exec STOREDPROCEDURE1 -- one result set

exec STOREDPROCEDURE2 -- second result set

End

HTH

0
 

Accepted Solution

by:
rsrsm earned 225 total points
ID: 9925625
One of the possible way is:
1st stored procedure:
create proc prc1 as
select col1 from tbl1

2nd procedure:
create proc prc2 as
select col2 from tbl2

3rd combined or union would be
create proc prc 3 as
begin
exec prc1
exec prc2
end

One draw back is tht result set displayed in 2 different grids and not next to each other.

RSRSM
0
 

Expert Comment

by:rsrsm
ID: 9925631
sorry forgot to add :
 execute the 3rd procedure run the command
exec prc3

RSRSM
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

824 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