Home All Groups Group Topic Archive Search About

Union with the results of two SP

Author
14 Sep 2006 2:08 PM
Robert Bravery
Hi all,
I've been asked the question of one can peform a union with the results of
two or more SP's

Thanks
Robert

Author
14 Sep 2006 2:29 PM
Tracy McKibben
Robert Bravery wrote:
> Hi all,
> I've been asked the question of one can peform a union with the results of
> two or more SP's
>
> Thanks
> Robert
>
>
>
>

Assuming the recordset returned by the two procedures has the same
structure, i.e. the same fields are returned, then you have a couple of
options:

1.  Insert the results of each sproc into seperate temp tables, and then
query/union the two temp tables together:

INSERT INTO #temp1
EXEC sproc1

INSERT INTO #temp2
EXEC sproc2

SELECT col1, col2, col3
FROM #temp1
UNION
SELECT col1, col2, col3
FROM #temp2


2.  Create a self-referencing linked server, and use OPENQUERY to
execute the sprocs:

SELECT col1, col2, col3
FROM OPENQUERY(servername, 'EXEC sproc1')
UNION
SELECT col1, col2, col3
FROM OPENQUERY(servername, 'EXEC sproc2')



--
Tracy McKibben
MCDBA
http://www.realsqlguy.com
Author
14 Sep 2006 2:42 PM
Robert Bravery
HI tracy and all,

Thanks for the quick response.
I would guess that the same could work fro reporting services

Thanks
Robert

Show quote
"Tracy McKibben" <tr***@realsqlguy.com> wrote in message
news:4509672E.90809@realsqlguy.com...
> Robert Bravery wrote:
> > Hi all,
> > I've been asked the question of one can peform a union with the results
of
> > two or more SP's
> >
> > Thanks
> > Robert
> >
> >
> >
> >
>
> Assuming the recordset returned by the two procedures has the same
> structure, i.e. the same fields are returned, then you have a couple of
> options:
>
> 1.  Insert the results of each sproc into seperate temp tables, and then
> query/union the two temp tables together:
>
> INSERT INTO #temp1
> EXEC sproc1
>
> INSERT INTO #temp2
> EXEC sproc2
>
> SELECT col1, col2, col3
> FROM #temp1
> UNION
> SELECT col1, col2, col3
> FROM #temp2
>
>
> 2.  Create a self-referencing linked server, and use OPENQUERY to
> execute the sprocs:
>
> SELECT col1, col2, col3
> FROM OPENQUERY(servername, 'EXEC sproc1')
> UNION
> SELECT col1, col2, col3
> FROM OPENQUERY(servername, 'EXEC sproc2')
>
>
>
> --
> Tracy McKibben
> MCDBA
> http://www.realsqlguy.com
Author
14 Sep 2006 2:48 PM
Tibor Karaszi
> I would guess that the same could work fro reporting services

You might need to add SET FMTONLY ON at the beginning of the TSQL batch for RS to be able to extract
,meta-data about the result set.

Show quote
"Robert Bravery" <m*@u.com> wrote in message news:%23pgwOtA2GHA.4976@TK2MSFTNGP02.phx.gbl...
> HI tracy and all,
>
> Thanks for the quick response.
> I would guess that the same could work fro reporting services
>
> Thanks
> Robert
>
> "Tracy McKibben" <tr***@realsqlguy.com> wrote in message
> news:4509672E.90809@realsqlguy.com...
>> Robert Bravery wrote:
>> > Hi all,
>> > I've been asked the question of one can peform a union with the results
> of
>> > two or more SP's
>> >
>> > Thanks
>> > Robert
>> >
>> >
>> >
>> >
>>
>> Assuming the recordset returned by the two procedures has the same
>> structure, i.e. the same fields are returned, then you have a couple of
>> options:
>>
>> 1.  Insert the results of each sproc into seperate temp tables, and then
>> query/union the two temp tables together:
>>
>> INSERT INTO #temp1
>> EXEC sproc1
>>
>> INSERT INTO #temp2
>> EXEC sproc2
>>
>> SELECT col1, col2, col3
>> FROM #temp1
>> UNION
>> SELECT col1, col2, col3
>> FROM #temp2
>>
>>
>> 2.  Create a self-referencing linked server, and use OPENQUERY to
>> execute the sprocs:
>>
>> SELECT col1, col2, col3
>> FROM OPENQUERY(servername, 'EXEC sproc1')
>> UNION
>> SELECT col1, col2, col3
>> FROM OPENQUERY(servername, 'EXEC sproc2')
>>
>>
>>
>> --
>> Tracy McKibben
>> MCDBA
>> http://www.realsqlguy.com
>
>
Author
14 Sep 2006 2:49 PM
Tibor Karaszi
That should have been SET FMTONLY OFF of course...

Show quote
"Robert Bravery" <m*@u.com> wrote in message news:%23pgwOtA2GHA.4976@TK2MSFTNGP02.phx.gbl...
> HI tracy and all,
>
> Thanks for the quick response.
> I would guess that the same could work fro reporting services
>
> Thanks
> Robert
>
> "Tracy McKibben" <tr***@realsqlguy.com> wrote in message
> news:4509672E.90809@realsqlguy.com...
>> Robert Bravery wrote:
>> > Hi all,
>> > I've been asked the question of one can peform a union with the results
> of
>> > two or more SP's
>> >
>> > Thanks
>> > Robert
>> >
>> >
>> >
>> >
>>
>> Assuming the recordset returned by the two procedures has the same
>> structure, i.e. the same fields are returned, then you have a couple of
>> options:
>>
>> 1.  Insert the results of each sproc into seperate temp tables, and then
>> query/union the two temp tables together:
>>
>> INSERT INTO #temp1
>> EXEC sproc1
>>
>> INSERT INTO #temp2
>> EXEC sproc2
>>
>> SELECT col1, col2, col3
>> FROM #temp1
>> UNION
>> SELECT col1, col2, col3
>> FROM #temp2
>>
>>
>> 2.  Create a self-referencing linked server, and use OPENQUERY to
>> execute the sprocs:
>>
>> SELECT col1, col2, col3
>> FROM OPENQUERY(servername, 'EXEC sproc1')
>> UNION
>> SELECT col1, col2, col3
>> FROM OPENQUERY(servername, 'EXEC sproc2')
>>
>>
>>
>> --
>> Tracy McKibben
>> MCDBA
>> http://www.realsqlguy.com
>
>
Author
14 Sep 2006 2:33 PM
Tibor Karaszi
Not directly... But there are (not so neat) workarounds:

INSERT INTO t1 (c1, c2, ...)
EXEC p1

INSERT INTO t2 (c1, c2, ...)
EXEC p2

SELECT ... FROM  t1
UNION
SELECT ... FROM  t1



SELECT ... FROM OPENROWSET(... 'p1')
UNION
SELECT ... FROM OPENROWSET(... 'p2')
Also see OPENQUERY,
Show quote
"Robert Bravery" <m*@u.com> wrote in message news:%23Zj1faA2GHA.4976@TK2MSFTNGP02.phx.gbl...
> Hi all,
> I've been asked the question of one can peform a union with the results of
> two or more SP's
>
> Thanks
> Robert
>
>
>
>
Author
14 Sep 2006 2:33 PM
Immy
You will need to execute both procs into a temp table first and then UNION
them after.
You could also insert both results into 1 temp table and select straight
from it without the UNION.
Immy
Show quote
"Robert Bravery" <m*@u.com> wrote in message
news:%23Zj1faA2GHA.4976@TK2MSFTNGP02.phx.gbl...
> Hi all,
> I've been asked the question of one can peform a union with the results of
> two or more SP's
>
> Thanks
> Robert
>
>
>
>

AddThis Social Bookmark Button