Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temporary tables in sql server

Tags:

sql

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;
like image 567
CPDS Avatar asked Aug 31 '11 14:08

CPDS


People also ask

Where the temporary tables are stored in SQL Server?

Temporary tables are stored in tempdb Database.

Why do we need temporary tables in SQL Server?

Temporary Tables are a great feature that lets you store and process intermediate results by using the same selection, update, and join capabilities that you can use with typical SQL Server tables. The temporary tables could be very useful in some cases to keep temporary data.

What is difference between temporary table and normal table?

A Temp table is easy to create and back up data. Table variable involves the effort when you usually create the normal tables. Temp table result can be used by multiple users. Table variable can be used by the current user only.

What is the difference between view and temporary table?

The main difference between them is that a table is an object that consists of rows and columns to store and retrieve data whenever the user needs it. In contrast, the view is a virtual table based on an SQL statement's result set and will disappear when the current session is closed.


1 Answers

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.

like image 174
Oded Avatar answered Oct 15 '22 02:10

Oded