临时表与创建和删除表
我正在创建一个表“InterviewTemp”,在那里插入数据,用该数据更新第二个表,然后删除“InterviewTemp”表。
有一个例子:
CREATE TABLE [entrevistasTemp](
[id_usuario] [int] NULL,
[id_entrevista] [int] NULL,
[comentarios] [varchar](300) NULL
)
INSERT [entrevistasTemp] ([id_usuario], [id_entrevista], [comentarios]) VALUES (12099, 4515, CONVERT(TEXT, N'Riesgo muy alto. Marun Victoria, '))
INSERT [entrevistasTemp] ([id_usuario], [id_entrevista], [comentarios]) VALUES (15347, 4516, CONVERT(TEXT, N'Riesgo muy alto. Marun Victoria, '))
UPDATE entrevistas
set entrevistas.comentarios = entrevistasTemp.comentarios
from entrevistasTemp
WHERE entrevistas.id = entrevistasTemp.id_entrevista
drop table entrevistasTemp
有更好的方法吗?
编辑:仅插入 4.5k 行
I'm creating a table "InterviewTemp" , inserting data there, updating a second table with that data and then dropping the "InterviewTemp" table.
there is an example:
CREATE TABLE [entrevistasTemp](
[id_usuario] [int] NULL,
[id_entrevista] [int] NULL,
[comentarios] [varchar](300) NULL
)
INSERT [entrevistasTemp] ([id_usuario], [id_entrevista], [comentarios]) VALUES (12099, 4515, CONVERT(TEXT, N'Riesgo muy alto. Marun Victoria, '))
INSERT [entrevistasTemp] ([id_usuario], [id_entrevista], [comentarios]) VALUES (15347, 4516, CONVERT(TEXT, N'Riesgo muy alto. Marun Victoria, '))
UPDATE entrevistas
set entrevistas.comentarios = entrevistasTemp.comentarios
from entrevistasTemp
WHERE entrevistas.id = entrevistasTemp.id_entrevista
drop table entrevistasTemp
there is a better way to do this?
EDIT: just inserting 4.5k rows
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建临时表而不是表:
Create a temporary table instead of a table:
仅根据插入的数据大小和访问行的频率来回答。
如果您有一个大型数据集,那么您可以创建一个表,将数据插入到该表中,然后在表中实现索引,然后使用该表进行任何进一步的操作,然后删除该表。
如果数据大小有限。那么最好选择 aF 的答案。
Answer solely depending upon size of data being inserted and frequency of rows being accessed.
If you have a large dataset then you can create a table insert data into that table then implement indexing in the table then use that table for any further operations and thereafter drop the table.
If data size is limited. Then going with answer by aF would be preferable.
表变量甚至比临时表(如果您的 SQL Server 版本支持它 [2005+])更好。当您创建临时表时,SQL Server必须在每次运行时重新编译查询。表变量不存在这个问题。它们也是在内存中而不是在磁盘上创建的,并且具有 更少的锁定和事务日志争用问题。
代码如下所示:
Even better than the temporary table (if your version of SQL Server supports it [2005+]) is a table variable. When you create a temporary table, SQL Server must recompile the query any time it runs. Table variables don't have this issue. They are also created in memory rather than on disk, and have fewer locking and transaction log contention issues.
Code would look like this: