[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 365
  • Last Modified:

Help using temporary tables and EXEC

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
baetha
Asked:
baetha
1 Solution
 
DennisHCommented:
It sounds like the table hasn't been created yet.  Try placing a GO after the Create Table statement.
0
 
SpaarQCommented:
Create the temporary table inside the body of your stored procedure..
0
 
baethaAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Stephen_FranceCommented:
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
 
baethaAuthor Commented:
I didn't know about the double-hash (##), but it doesn't solve my problem.
0
 
trillian30Commented:
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
 
KirkGrayCommented:
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
 
formulaCommented:
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
 
baethaAuthor Commented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now