Showing posts with label temporary. Show all posts
Showing posts with label temporary. Show all posts

Monday, March 19, 2012

Getting results of stored proc into temporary table

I have a need to get the results of a stored procedure into a temporary
table. The results of the query will be larger than 8000 characters so I
can't use a variable. Following is what is being done in the stored
procedure (findorphans):
==========
PRINT 't1'
SELECT mco_itn,chg_dt,chg_in FROM t1 LEFT JOIN t2 ON t2.mco_itn=t1.mco_itn
WHERE t2.mco_itn IS NULL
PRINT 't3'
SELECT mco_itn,chg_dt,chg_in FROM t3 LEFT JOIN t4 ON t4.mco_itn=t3.mco_itn
WHERE t4.mco_itn IS NULL
==========
I tried the following:
==========
CREATE TABLE ##msgtext (msgtext text)
INSERT INTO ##msgtext exec test..mxw_findorphans
...
DROP TABLE ##msgtext
==========
I end up getting an error as follows:
==========
t1
Server: Msg 213, Level 16, State 7, Procedure findorphans, Line 2
Insert Error: Column name or number of supplied values does not match table
definition
==========
Ultimately I am going to send the results in an email. I had it working
fine but the messages were getting cutoff due to the 8000 character limit.
In the documentation for xp_sendmail they had an example of sending a
message > 8000 which is where I am going with this. I think the problem may
be that line 2 returns 0 rows and for some reason is causing the problem.
Can anyone point me at what I am doing wrong?
TIA
MarkMark,
your temp table need to mirror exactly the columns the stored procedures
will return, so looking at it, your temp table should contain columns
mco_itn,chg_dt,chg_in
I'm also not sure if you can insert multiple resultsets into a temp table.
so you may have to make the queries in the stored procedure into a UNION.
Have you considered a user-defined function which can return a TABLE ? Look
them up in BOL.
Hope that helps.
Damien
"Mark Butler" wrote:

> I have a need to get the results of a stored procedure into a temporary
> table. The results of the query will be larger than 8000 characters so I
> can't use a variable. Following is what is being done in the stored
> procedure (findorphans):
> ==========
> PRINT 't1'
> SELECT mco_itn,chg_dt,chg_in FROM t1 LEFT JOIN t2 ON t2.mco_itn=t1.mco_itn
> WHERE t2.mco_itn IS NULL
> PRINT 't3'
> SELECT mco_itn,chg_dt,chg_in FROM t3 LEFT JOIN t4 ON t4.mco_itn=t3.mco_itn
> WHERE t4.mco_itn IS NULL
> ==========
> I tried the following:
> ==========
> CREATE TABLE ##msgtext (msgtext text)
> INSERT INTO ##msgtext exec test..mxw_findorphans
> ....
> DROP TABLE ##msgtext
> ==========
> I end up getting an error as follows:
> ==========
> t1
> Server: Msg 213, Level 16, State 7, Procedure findorphans, Line 2
> Insert Error: Column name or number of supplied values does not match tabl
e
> definition
> ==========
> Ultimately I am going to send the results in an email. I had it working
> fine but the messages were getting cutoff due to the 8000 character limit.
> In the documentation for xp_sendmail they had an example of sending a
> message > 8000 which is where I am going with this. I think the problem m
ay
> be that line 2 returns 0 rows and for some reason is causing the problem.
> Can anyone point me at what I am doing wrong?
> TIA
> Mark
>
>|||Looks like you can have multiple resultsets from a procedure:
CREATE PROC usp_test
AS
SELECT 1
SELECT 2
GO
DROP TABLE #test
CREATE TABLE #test ( value1 INT )
GO
INSERT INTO #test
EXEC usp_test
SELECT *
FROM #test
DROP PROC usp_test
GO
"Damien" wrote:
> Mark,
> your temp table need to mirror exactly the columns the stored procedures
> will return, so looking at it, your temp table should contain columns
> mco_itn,chg_dt,chg_in
> I'm also not sure if you can insert multiple resultsets into a temp table.
> so you may have to make the queries in the stored procedure into a UNION.
> Have you considered a user-defined function which can return a TABLE ? Lo
ok
> them up in BOL.
> Hope that helps.
>
> Damien
> "Mark Butler" wrote:
>|||When I send the output from the stored procedure directly into xp_sendmail
it is a text based message formated like the following:
=========
t1
mco_itn chg_dt chg_in
-- -- --
t3
mco_itn chg_dt chg_in
-- -- --
=========
I was expecting that to be true the way I was doing it but as you pointed
out that must be false.
Is there another or better way?
Do I need to CAST and concatinate the fields so they are one?
Is there a way to send the results to a text file that I can then attatch to
the email?
Thanx for any suggestions.
Mark
"Damien" <Damien@.discussions.microsoft.com> wrote in message
news:7F9C8D7E-06CF-4272-A690-34C86EC517D3@.microsoft.com...
> Mark,
> your temp table need to mirror exactly the columns the stored procedures
> will return, so looking at it, your temp table should contain columns
> mco_itn,chg_dt,chg_in
> I'm also not sure if you can insert multiple resultsets into a temp table.
> so you may have to make the queries in the stored procedure into a UNION.
> Have you considered a user-defined function which can return a TABLE ?
> Look
> them up in BOL.
> Hope that helps.
>
> Damien
> "Mark Butler" wrote:
>

Getting records output from stored procedure into a table

I have a stored procedure that returns two sets of records, and I want to
execute the procedure and insert the second set of records into a temporary
table outside the procedure.
The last lines of the stored procedure look like this:
--Return count of programs first
SELECT COUNT(*)
FROM #Programs
--Return programs
SELECT *
FROM #Programs
ORDER BY
Day1Date
I have tried the following but this only captures the first set of records
(the count):
INSERT INTO #temp EXECUTE StoredProc
I cannot change the stored procedure. Is there a way to get the second set
into a temprary table (or even permanent table, table variable etc) ?This is a multi-part message in MIME format.
--=_NextPart_000_0067_01C3C3BB.8715A830
Content-Type: text/plain;
charset="Windows-1252"
Content-Transfer-Encoding: 7bit
Consider using an output parameter to store the count:
-- inside the called proc
SELECT @.count = COUNT(*)
FROM #Programs
-- inside the calling proc
declare @.count int
INSERT INTO #temp
EXECUTE StoredProc @.count output
Tom
---
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinnaclepublishing.com/sql
"Laurence Neville" <laurenceneville@.hotmail.com> wrote in message
news:OSwOuR#wDHA.3116@.TK2MSFTNGP11.phx.gbl...
I have a stored procedure that returns two sets of records, and I want to
execute the procedure and insert the second set of records into a temporary
table outside the procedure.
The last lines of the stored procedure look like this:
--Return count of programs first
SELECT COUNT(*)
FROM #Programs
--Return programs
SELECT *
FROM #Programs
ORDER BY
Day1Date
I have tried the following but this only captures the first set of records
(the count):
INSERT INTO #temp EXECUTE StoredProc
I cannot change the stored procedure. Is there a way to get the second set
into a temprary table (or even permanent table, table variable etc) ?
--=_NextPart_000_0067_01C3C3BB.8715A830
Content-Type: text/html;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&

Consider using an output parameter to =store the count:
-- inside the called =proc
SELECT @.count =3D =COUNT(*)FROM #Programs
-- inside the calling =proc
declare @.count int
INSERT INTO #temp
EXECUTE StoredProc @.count output
-- Tom
---T=homas A. Moreau, BSc, PhD, MCSE, MCDBASQL Server MVPColumnist, SQL =Server ProfessionalToronto, ON Canadahttp://www.pinnaclepublishing.com/sql">www.pinnaclepublishing.com=/sql
"Laurence Neville" wrote in message news:OSwOuR#wDHA.3116=@.TK2MSFTNGP11.phx.gbl...I have a stored procedure that returns two sets of records, and I want toexecute the procedure and insert the second set of records into a temporarytable outside the procedure.The last lines of the =stored procedure look like this:--Return count of programs =firstSELECT COUNT(*)FROM #Programs--Return programsSELECT *FROM #ProgramsORDER BYDay1DateI have tried the following but =this only captures the first set of records(the count):INSERT =INTO #temp EXECUTE StoredProcI cannot change the stored procedure. Is there =a way to get the second setinto a temprary table (or even permanent table, =table variable etc) ?

--=_NextPart_000_0067_01C3C3BB.8715A830--