Temporary tables in sql server

随声附和 提交于 2019-12-10 02:15:45

问题


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;

回答1:


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.




回答2:


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.




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/7258651/temporary-tables-in-sql-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!