Solved

Creating a Temp Table from a Stored Procedure

Posted on 2011-02-13
13
755 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
Comment Utility
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
Comment Utility
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
Comment Utility
Have you tried this command?

SELECT * INTO #MY_TEMP_TABLE EXEC dbo.MY_OLD_STORED_PROC 'value1','value2','value3','value4'
0
 

Author Comment

by:alfardan
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:alfardan
Comment Utility
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
Comment Utility
yes , it will be "slower" than a normal direct execution of the procedure into a table
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
Did you check global temp tables?
0
 

Author Comment

by:alfardan
Comment Utility
Sharath_123

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

Author Comment

by:alfardan
Comment Utility
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
Comment Utility
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
Comment Utility
Perfect to the point, thank you Sharath_123.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

13 Experts available now in Live!

Get 1:1 Help Now