CPDS
CPDS

Reputation: 597

Temporary tables in sql server

I am working on creation of temporary tables in sql server. I created the temporary table successfully but when I try to view the data it says INVALID OBJECT NAME. Can anyone pls tell foe how long temporary tables exists? If I am logged on to sql server as userid - devloper and pwd = 0999 and someother person is also logged on to the sql server with same credentials, this temporary tables will get deleted? my sql is as follows:

SELECT net_node_model.SYS_ID, net_node_model.NODE, mst_temp_equation.TEMP_ID,
       mst_temp_equation.EQ_ID
INTO ##NT_MASTER_TEMP_EQUATION
FROM mst_temp_equation INNER JOIN
     net_node_model ON mst_temp_equation.TEMP_ID = net_node_model.TEMP_ID
GROUP BY net_node_model.SYS_ID, net_node_model.NODE, mst_temp_equation.TEMP_ID,
         mst_temp_equation.EQ_ID, mst_temp_equation.EQ_NAME,
         mst_temp_equation.EQ_TYPE, mst_temp_equation.[OBJECT],
         mst_temp_equation.VAR_TYPE, mst_temp_equation.VAR_NAME,
         mst_temp_equation.VAR_SUBSET, mst_temp_equation.VAR_SET,
         mst_temp_equation.RHS_RELN, mst_temp_equation.RHS_OBJECT,
         mst_temp_equation.RHS_VAR_SET, mst_temp_equation.RHS_VAR_SUBSET,
         mst_temp_equation.RHS_VAR_TYPE, mst_temp_equation.RHS_VAR_NAME,
         mst_temp_equation.EQ_TP_OFFSET, mst_temp_equation.RHS_TP_OFFSET,
         mst_temp_equation.RETAIN, mst_temp_equation.TIME_PRD,
         mst_temp_equation.EQ_VAR_SUBTYPE, mst_temp_equation.RHS_VAR_SUBTYE;

Upvotes: 11

Views: 14575

Answers (4)

Shiwangini
Shiwangini

Reputation: 836

Here seems you have created global temporary table. Note that Global temporary tables are useful when you want the result set visible to all other sessions. No need to setup permissions. Anyone can insert values, modify, or retrieve records from the table. Also note that anyone can DROP the table.

And once the session disconnects these tables get dropped automatically.

Upvotes: 0

PaulStock
PaulStock

Reputation: 11283

If this is the exact query you used, then I think you may have a syntax error on the first line, looks like there is an extra comma right before the keyword INTO

...mst_temp_equation.EQ_ID,  INTO ##NT_MASTER_TEMP_EQUATION
                          ^

Not sure if this is causing the INVALID OBJECT NAME error or not

Upvotes: 0

MatBailie
MatBailie

Reputation: 86798

Are you saying that you have already created the ##NT_MASTER_TEMP_EQUATION table, and are now trying to insert into it? If so, use the syntax INSERT INTO ##NT_MASTER_TEMP_EQUATION SELECT ... instead of what you have.

SELECT ... INTO ##temp FROM ... is used to both create the table AND populate it.


Additionally, you have a rogue , at the end of your SELECT list (just before the keyword INTO). This should be removed.

Upvotes: 0

Oded
Oded

Reputation: 499352

If you are using a regular temporary table #table, it will not be visible to any other session apart from the one it was created on. Once that session is finished, the table will be removed.

If you are using a global temporary table ##table, it will be visible to other sessions.

From MSDN - CREATE TABLE, under temporary tables:

Global temporary tables are automatically dropped when the session that created the table ends and all other tasks have stopped referencing them.

Upvotes: 20

Related Questions