[英]SQL Server: how to insert to temporary table?
I have one Temporary Table 我有一张临时桌子
CREATE TABLE #TEMP (TEMP_ID INT IDENTITY(1,1))
And I would like to insert records to that table, How can I?I do as follow: 我想将记录插入到该表中,我该怎么做?
INSERT INTO #TEMP DEFAULT VALUES
But sometimes it doesn't work. 但是有时它不起作用。 What it might be?And I would like to know lifetime of temptable in SQL Server. 可能是什么?我想知道SQL Server中的temptable生命周期。 Please Help me! 请帮我!
Thanks all! 谢谢大家!
Works for me! 为我工作!
CREATE TABLE #TEMP (TEMP_ID INT IDENTITY(1,1))
--And I would like to insert records to that table, How can I?I do as follow:
INSERT INTO #TEMP DEFAULT VALUES
INSERT INTO #TEMP DEFAULT VALUES
INSERT INTO #TEMP DEFAULT VALUES
INSERT INTO #TEMP DEFAULT VALUES
select * from #TEMP
Gives: 得到:
TEMP_ID
1
2
3
4
Keep in mind it needs to be the same "batch" or single query etc. 请记住,它必须是相同的“批”或单个查询等。
PK :-) PK :-)
Not sure what you mean about "sometimes it doesn't work." 不知道您对“有时不起作用”的含义。
However, a local temp table (a single #) lifetime is the current session or scope (such as the stored proc or function duration). 但是,本地临时表(单个#)的生存期是当前会话或作用域(例如存储的proc或函数的持续时间)。 CREATE TABLE on MSDN as a lot more with examples in the section "Temporary Tables" 在MSDN上创建表的更多信息,请参见“临时表”部分中的示例
That looks fine. 看起来不错。 Also INSERT INTO #TEMP (TEMP_ID) VALUES (DEFAULT)
. 还要INSERT INTO #TEMP (TEMP_ID) VALUES (DEFAULT)
。 When you say that sometimes it doesn't work, what error are you getting? 当您说有时它不起作用时,您遇到什么错误? # tables only have a lifetime and scope of your session. #个表仅具有会话的生存期和范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.