Solved

Help using temporary tables and EXEC

Posted on 1998-10-01
9
353 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
Comment Utility
It sounds like the table hasn't been created yet.  Try placing a GO after the Create Table statement.
0
 

Expert Comment

by:SpaarQ
Comment Utility
Create the temporary table inside the body of your stored procedure..
0
 

Author Comment

by:baetha
Comment Utility
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
 

Expert Comment

by:Stephen_France
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

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

Expert Comment

by:trillian30
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

771 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

11 Experts available now in Live!

Get 1:1 Help Now