Solved

Help using temporary tables and EXEC

Posted on 1998-10-01
9
359 Views
Last Modified: 2010-03-19
Here is what I am trying to accomplish...
frist I am creating a temp table
   drop table #TRCTempTable
   go
   create table #TRCTempTable(doc_set_id int,
                              consultant_num int)
Then creating a stored procedure which is passed an SQL string...
   CREATE PROCEDURE FirstQuery(@sqlstr text) AS
   EXEC(@sqlstr)
   SELECT TT.*,CL.name
   FROM   consultant_lkup CL,#TRCTempTable TT
   WHERE  CL.consultant_num =* TT.consultant_num
Then problem is, I keep getting the error:
   Msg 208, Level 16, State 1
   Invalid object name '#TRCTempTable'.
I think the problem exists in the EXEC statement, but I'm not sure.  I need to find a work around.
0
Comment
Question by:baetha
9 Comments
 
LVL 1

Expert Comment

by:DennisH
ID: 1090367
It sounds like the table hasn't been created yet.  Try placing a GO after the Create Table statement.
0
 

Expert Comment

by:SpaarQ
ID: 1090368
Create the temporary table inside the body of your stored procedure..
0
 

Author Comment

by:baetha
ID: 1090369
Sorry, but that doesn't help, because I was actually running the first part manually, the "GO" is just a syntax that I neglected to show in my original question.  I drop and create the temptable and then I run the procedure from the web application, it acts as though the temptable doesn't exist.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:Stephen_France
ID: 1090370
I think this is because your temporary table only has local scope.
To define a temporary table with scope outside where it is defined use a double hash.
i.e.drop table ##TRCTempTable
   go
   create table ##TRCTempTable(doc_set_id int,
                              consultant_num int)

0
 

Author Comment

by:baetha
ID: 1090371
I didn't know about the double-hash (##), but it doesn't solve my problem.
0
 

Expert Comment

by:trillian30
ID: 1090372
If you are trying to drop a table and want to check for it's existence first use:

if exists (select * from sysobjects where id = object_id('dbo.TRCTempTable') and sysstat & 0xf = 3)
      drop table dbo.TRCTempTable

Why are you using a temporary table if you are creating and dropping it outside of the store procedure?

0
 

Expert Comment

by:KirkGray
ID: 1090373
I think the problem is due to a misuse of the # operator...  This creates a temp table that that has a lifetime only as long as a connection is open.  When you create a table with #, SQL Server maps it to a different but always unique name for that connection.  So you can't create a #table in one query and call it in another, that won't work 'cause by the time you get to the second query the first table will be way gone.. and if it's not i doubt sql server will let you see it anyhow...  I think what you need to do is get some code that creates a unique tablename on the spot, and create it as a normal table that will persist.
0
 
LVL 2

Accepted Solution

by:
formula earned 100 total points
ID: 1090374
You cannot create a temporary table(#) or a global temporary(##) and then use it in a stored procedure unless you create the table within the stored procedure, something like this in your example:

CREATE PROCEDURE FirstQuery @sqlstr text
AS
   EXEC(@sqlstr)

   create table #TRCTempTable(doc_set_id int, consultant_num int)
/* insert your records to temp
   insert into #TRCTempTable values (1,2)
*/
   SELECT TT.*,CL.name
   FROM   consultant_lkup CL,#TRCTempTable TT
   WHERE  CL.consultant_num =* TT.consultant_num
go

In the case of the global temp(##), it only remains as long as the session that created it remains, which is why it doesn't work for you.

Depending upon your application, you may not be able to do this.
Your alternative is to create the table as a permanent table, and use the permanent table in the stored procedure, like this:

   create table tempdb..TRCTempTable(doc_set_id int,
                                 consultant_num int)

CREATE PROCEDURE FirstQuery @sqlstr text
AS
   EXEC(@sqlstr)

   SELECT TT.*,CL.name
   FROM   consultant_lkup CL,tempdb..TRCTempTable TT
   WHERE  CL.consultant_num =* TT.consultant_num
go


Also if your always changing values in this table, instead of drop and recreate, do something like this:

truncate table tempdb..TRCTempTable
insert....



0
 

Author Comment

by:baetha
ID: 1090375
Thanks, and sorry for the delay.  This information helps, elthough we have had to change gears, and I'm not sure whether we will be using the same scenario or not.  I might be posting another question later...  who knows.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

730 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