SQL Server:如何插入临时表?
我有一个临时表
CREATE TABLE #TEMP (TEMP_ID INT IDENTITY(1,1))
,我想向该表插入记录,我该怎么办?我按如下操作:
INSERT INTO #TEMP DEFAULT VALUES
但有时它不起作用。它可能是什么?我想知道 SQL Server 中 temptable 的生命周期。请帮我!
谢谢大家!
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. Please Help me!
Thanks all!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不知道你所说的“有时不起作用”是什么意思。
但是,本地临时表(单个 #)生命周期是当前会话或范围(例如存储过程或函数持续时间)。 在 MSDN 上创建表,其中包含“更多示例”临时表”
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). CREATE TABLE on MSDN as a lot more with examples in the section "Temporary Tables"
对我有用!
给出:
记住它需要是相同的“批次”或单个查询等。
PK :-)
Works for me!
Gives:
Keep in mind it needs to be the same "batch" or single query etc.
PK :-)
看起来不错。另外
INSERT INTO #TEMP (TEMP_ID) VALUES (DEFAULT)
。当你说有时它不起作用时,你会遇到什么错误? # 表只有会话的生命周期和范围。That looks fine. Also
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.