Solved

Simple SQL Union Problem

Posted on 2003-12-11
7
1,123 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
[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
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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

734 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