SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


insert output to temp table


insert output to temp table

Author
Message
curious_sqldba
curious_sqldba
SSCertifiable
SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)

Group: General Forum Members
Points: 7450 Visits: 3696
declare @SQLString varchar(max)

set @SQLString='EXEC dbo.mysproc ''ABC'''


insert INTO #TEMP_table
exec (@SQLString)



table has to be created on the fly. Any thoughts please?
Steve Jones
Steve Jones
SSC Guru
SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)

Group: Administrators
Points: 147207 Visits: 19434
Why does the table have to be created on the fly? You know the output of the proc already, so create the temp table using that basis.

If you say you want one routine for all procs, you're being lazy. Create routines for each proc that needs this.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Lowell
Lowell
SSC Guru
SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)

Group: General Forum Members
Points: 72324 Visits: 40942
to capture the results form a stored procedure, the table must exist, with all the columns defined;


then you can do something like this:

IF NOT EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'WHO_DATA') AND xtype in (N'U'))
CREATE TABLE WHO_DATA (
[WHORESULTSID] INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
[SPID] CHAR (5) NULL,
[INSERTDT] DATETIME NULL DEFAULT(GETDATE()),
[STATUS] VARCHAR(30) NULL,
[LOGIN] VARCHAR(30) NULL,
[HOSTNAME] VARCHAR(30) NULL,
[BLKBY] VARCHAR(30) NULL,
[DBNAME] VARCHAR(30) NULL,
[COMMAND] VARCHAR(30) NULL,
[CPUTIME] INT NULL,
[DISKIO] INT NULL,
[LASTBATCH] VARCHAR(30) NULL,
[PROGRAMNAME] VARCHAR(200) NULL,
[SPIDINT] INT NULL,
[REQUESTID] INT NULL
)
--table exists, insert some data
INSERT INTO WHO_DATA(SPID,Status,Login,HostName,BlkBy,DBName,Command,CPUTime,DiskIO,LastBatch,ProgramName,SPIDINT,REQUESTID)
EXEC sp_who2



there's a bit of a work around you can try by using openrowset:, it tends to be a LOT slower, but it works:

--database name required
SELECT *
INTO #tmp
FROM OPENROWSET( 'SQLNCLI',
'Server=(local);Trusted_Connection=yes;',
'SET FMTONLY OFF; SET NOCOUNT ON; exec master.sys.sp_who'
)




Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
curious_sqldba
curious_sqldba
SSCertifiable
SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)

Group: General Forum Members
Points: 7450 Visits: 3696
Steve Jones - SSC Editor (7/3/2012)
Why does the table have to be created on the fly? You know the output of the proc already, so create the temp table using that basis.

If you say you want one routine for all procs, you're being lazy. Create routines for each proc that needs this.


Thanks. I am not a developer. I am just trying to come up a solution based on the requirement. I am sure we should go and optimize our queries. This is for a report. You have a sproc to generate report, if you have to query the output of the sproc how would you do that? I would still like to know if the method which i stated is possible or not.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62975 Visits: 17959
sqldba_newbie (7/3/2012)
Steve Jones - SSC Editor (7/3/2012)
Why does the table have to be created on the fly? You know the output of the proc already, so create the temp table using that basis.

If you say you want one routine for all procs, you're being lazy. Create routines for each proc that needs this.


Thanks. I am not a developer. I am just trying to come up a solution based on the requirement. I am sure we should go and optimize our queries. This is for a report. You have a sproc to generate report, if you have to query the output of the sproc how would you do that? I would still like to know if the method which i stated is possible or not.


This whole thing just sounds like a monster kludge in the making. I see no reason for the dynamic sql at all and creating a generic proc to return data into an "on the fly" type of temp table so you can query it sounds like the process needs a lot of help. Why does a report need to take the results of a proc and then only get some of the rows? Why not change the original proc to return the data that you need?

To directly answer your question, no you can't do this like you presented.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Steve Jones
Steve Jones
SSC Guru
SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)

Group: Administrators
Points: 147207 Visits: 19434
I don't think it's possible. Usually a sproc has a known result set, even for a report, so you would have the values being returned.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Orlando Colamatteo
Orlando Colamatteo
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39964 Visits: 14412
Lowell (7/3/2012)
there's a bit of a work around you can try by using openrowset:, it tends to be a LOT slower, but it works:

--database name required
SELECT *
INTO #tmp
FROM OPENROWSET( 'SQLNCLI',
'Server=(local);Trusted_Connection=yes;',
'SET FMTONLY OFF; SET NOCOUNT ON; exec master.sys.sp_who'
)





It should be noted that using this technique requires us to enable Ad Hoc Distributed Queries. The option is disabled by default.

-- view your settings
EXEC sys.sp_configure
@configname = 'Ad Hoc Distributed Queries';



Please see the Security Note in BOL:

Ad Hoc Distributed Queries Option (SQL Server 2005)

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
curious_sqldba
curious_sqldba
SSCertifiable
SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)

Group: General Forum Members
Points: 7450 Visits: 3696
opc.three (7/3/2012)
Lowell (7/3/2012)
there's a bit of a work around you can try by using openrowset:, it tends to be a LOT slower, but it works:

--database name required
SELECT *
INTO #tmp
FROM OPENROWSET( 'SQLNCLI',
'Server=(local);Trusted_Connection=yes;',
'SET FMTONLY OFF; SET NOCOUNT ON; exec master.sys.sp_who'
)

this worked. thanks





It should be noted that using this technique requires us to enable Ad Hoc Distributed Queries. The option is disabled by default.

-- view your settings
EXEC sys.sp_configure
@configname = 'Ad Hoc Distributed Queries';



Please see the Security Note in BOL:

Ad Hoc Distributed Queries Option (SQL Server 2005)

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search