Solved

Simple SQL Union Problem

Posted on 2003-12-11
7
1,113 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

746 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

10 Experts available now in Live!

Get 1:1 Help Now