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
Solved

Creating a Temp Table from a Stored Procedure

Posted on 2011-02-13
13
762 Views
Last Modified: 2012-05-11
I have a procedure (A) that produces a table and I would like to create a new procedure (B) that creates a temp table that stores all columns structures and row values produced by procedure (A).

How to do so?

IMPORTANT NOTE: I don't want to create my temp table column by column, becasue the table that procedure (A) changes its structure according to some conditions, as well as any table this procedure (A) produces contains so many columns.

I tried this IN PROCEDURE (b) but it failed:

INSERT INTO #MY_TEMP_TABLE EXEC dbo.MY_OLD_STORED_PROC 'value1','value2','value3','value4'

SELECT COUNT(*) FROM #MY_TEMP_TABLE


The error given is: Invalid object name '#MY_TEMP_TABLE'

Please advise
0
Comment
Question by:alfardan
  • 6
  • 3
  • 2
  • +2
13 Comments
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 34885747
I'm not sure if this totally answers your question, but to do what your code is suggesting you would do the following:

: Outer Context (can be a stored proc, query analyser, a connection from an application etc)

create table #Temp / insert into #temp -- construct the temp table as you prefer

: Proc A

insert into #Temp

: Proc B

select from #Temp
0
 
LVL 40

Expert Comment

by:Sharath
ID: 34885796
Create global temp tables.

-- 1st sp
create proc a as
create table ##test (col int)
go

Open in new window

-- 2nd sp
create proc b as 
insert ##test values (1)
select * from ##test
go

Open in new window

-- execute the SPs
exec a
exec b

Open in new window

0
 
LVL 5

Expert Comment

by:karthika_cts
ID: 34886005
Have you tried this command?

SELECT * INTO #MY_TEMP_TABLE EXEC dbo.MY_OLD_STORED_PROC 'value1','value2','value3','value4'
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

Author Comment

by:alfardan
ID: 34886225
karthika_cts

I just tried it and it gives me this error:

Msg 263, Level 16, State 1, Line 1
Must specify table to select from.
Msg 1038, Level 15, State 5, Line 1
An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Add a name or single space as the alias name.
0
 

Author Comment

by:alfardan
ID: 34886362
dale_burrell

What I want is the following ni more details:

I have a procedure called (proc_a)

This procedure produces the following table att1
I want now to create a new procedure that uses the table data that was created by proc_a to ultimately produce a differet table.

Please have in the mind the following: the table that proc_a produces contains a count of columns that differ according to the parameters passed to it. Therefore, I can not create a temp table with specific columns matching the table columns produced by proc_a because as I said the structure of this produced table columns count differ according to the parametters passed to proc_a.

Hopefully this illustrates more.
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 34886694
you can't really do ir without creating the table before hand...

if you want to accept the overhead, and can get the dba to accept the need then you can do it with OPENQUERY

create a linked server pointing to the current server instance

then execute the procedure via an openquery statement using the liked server

e.g.

select c.*
   into #temp
 from OpenQuery(linkedserver,'EXEC PROCEDURE....') as c
0
 

Author Comment

by:alfardan
ID: 34888404
Lowfatspread

What overhead? You mean this "OpenQuery" will or might cause a performance issue or resource overutilization on the server that this DB resides in?
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 34888469
yes , it will be "slower" than a normal direct execution of the procedure into a table
0
 
LVL 40

Expert Comment

by:Sharath
ID: 34891169
Did you check global temp tables?
0
 

Author Comment

by:alfardan
ID: 34893698
Sharath_123

Won't work for me because I have no authority to alter procedure 1
0
 

Author Comment

by:alfardan
ID: 34893730
Lowfatspread

I tried it as you suggested like this:

select c.* into #temp from OpenQuery(MY_SERVER_NAME,'exec dbo.PROC_A ''val1'',''val2'',''val3'',''val4''') as c

Open in new window


And I got the following error message:

Msg 7357, Level 16, State 2, Line 1
Cannot process the object "exec dbo.PROC_A 'val1','val2','val3','val4'". The OLE DB provider "SQLNCLI" for linked server "MY_SERVER_NAME" indicates that either the object has no columns or the current user does not have permissions on that object.

Open in new window



0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 34903793
Execute the SP with 3-part name.

select c.* into #temp from OpenQuery(MY_SERVER_NAME,'exec yourDBName.dbo.PROC_A ''val1'',''val2'',''val3'',''val4''') as c

Open in new window

0
 

Author Closing Comment

by:alfardan
ID: 34935534
Perfect to the point, thank you Sharath_123.
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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