简体   繁体   English

如何在SQL Server中将一列从一个表复制到另一表

[英]How can I copy one column to from one table to another in SQL Server

I use SQL Server 2008 R2 and my problem is this with a database called LogStats. 我使用SQL Server 2008 R2,而我的问题是数据库名为LogStats。

If I execute this query: 如果执行此查询:

select *
from ExceptionRow
     inner join HashFP ON ExceptionRow.Hash=HashFP.FingerPrintMD5
where ExceptionRow.Message is null
      AND not HashFP.MessageFP is null 

I got 126708 hits and it take 2.05 minutes. 我获得了126708次点击,它需要2.05分钟。 But optimizations is not my question. 但是优化不是我的问题。

I want to copy data from HashFP.MessageFP to ExceptionRow.Message without overwriting any data. 我想将数据从HashFP.MessageFP复制到ExceptionRow.Message而不覆盖任何数据。 I try this: 我尝试这样:

UPDATE ExceptionRow
SET Exceptionrow.Message = HashFP.MessageFP
FROM ExceptionRow 
     INNER JOIN HashFP ON ExceptionRow.Hash=HashFP.FingerPrintMD5
WHERE ExceptionRow.Message IS NULL
      AND NOT HashFP.MessageFP IS NULL

The result: 结果:

Msg 9002, Level 17, State 4, Line 1 The transaction log for database 'LogStats' is full. 消息9002,级别17,状态4,行1数据库'LogStats'的事务日志已满。 To find out why space in the log cannot be reused, see the log_reuse_wait_desc column in sys.databases 要找出为什么无法重用日志中的空间的原因,请参阅sys.databases中的log_reuse_wait_desc列

I tried this: 我尝试了这个:

SELECT name,log_reuse_wait_desc FROM sys.databases

From the result 从结果

tempdb   ACTIVE_TRANSACTION
LogStats ACTIVE_TRANSACTION

How can I abort those active transactions so the task can be successful ? 我如何才能中止那些活动的事务,以便任务能够成功完成?

How can I abort those active transactions so the task can be successful ? 我如何才能中止那些活动的事务,以便任务能够成功完成?

You can't, because it's the UPDATE FROM transaction. 您不能,因为它是UPDATE FROM事务。
You can either increase max size of the log file: 您可以增加日志文件的最大大小:

ALTER DATABASE DB_NAME
MODIFY FILE (NAME=LOG_FILE_NAME,MAXSIZE=UNLIMITED);

Or you can try something like this: 或者,您可以尝试执行以下操作:

WHILE EXISTS
(select *
from ExceptionRow
     inner join HashFP ON ExceptionRow.Hash=HashFP.FingerPrintMD5
where ExceptionRow.Message is null
      AND not HashFP.MessageFP is null
)
UPDATE TOP (1000) ExceptionRow
SET Exceptionrow.Message = HashFP.MessageFP
FROM ExceptionRow 
     INNER JOIN HashFP ON ExceptionRow.Hash=HashFP.FingerPrintMD5
WHERE ExceptionRow.Message IS NULL
      AND NOT HashFP.MessageFP IS NULL

IF the database has SIMPLE recovery model this should work, if FULL or BULK_LOAD you need also do backup of transaction log in every iteration. 如果数据库具有SIMPLE恢复模型,则该模型应该起作用,如果FULL或BULK_LOAD,则还需要在每次迭代中备份事务日志。

UPDATE ExceptionRow SET Exceptionrow.Message = HashFP.MessageFP FROM ExceptionRow 
INNER JOIN HashFP ON ExceptionRow.Hash=HashFP.FingerPrintMD5
WHERE ExceptionRow.Message IS NULL AND HashFP.MessageFP IS NOT NULL

or 要么

UPDATE ExceptionRow SET Exceptionrow.Message = HashFP.MessageFP FROM ExceptionRow 
INNER JOIN HashFP ON ExceptionRow.Hash=HashFP.FingerPrintMD5
WHERE ExceptionRow.Message IS NULL 

since you are going to update ExceptionRow.Message when null with HashFP.MessageFP we no need to bother whether HashFP.MessageFP have null in it.. 由于您将使用HashFP.MessageFP在null时更新ExceptionRow.Message因此我们无需理会HashFP.MessageFP是否包含null。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将列从一个表复制到 SQL 服务器中的另一个表 - How to copy column from one table into another table in SQL Server 如何将一列从一个表复制到另一个表 - How can I copy a column from one table to another 如何在SQL Server的同一张表中显示XML节点内容从一列到另一列? - How can I display XML node content from one column into another column on the same table in SQL Server? 我可以将数据从一个数据库表复制到另一个已经存在的数据库表sql服务器吗? - Can i copy data from one database table to another already existing database table sql server? 将一列从数据库表复制到另一数据库表sql server中的另一列 - Copy one column from a database table to another column in another database table sql server 如何将列从一个表复制到 sql 中的另一个表 - How do I copy a column from one table to another table in sql 如何在 SQL 服务器中将索引从一个表复制到另一个表 - How to copy indexes from one table to another in SQL Server 如何将一行从一个 SQL Server 表复制到另一个表 - How to copy a row from one SQL Server table to another SQL - 将数据从一个表列复制到另一个表列 - SQL - copy data from one table column to another table column 在 SQL 中,如何将一整列从一个表复制到另一个没有任何相互列或值的表? - how do i copy one whole column from a table to another without any mutual columns or values in SQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM