繁体   English   中英

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

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

我使用SQL Server 2008 R2,而我的问题是数据库名为LogStats。

如果执行此查询:

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

我获得了126708次点击,它需要2.05分钟。 但是优化不是我的问题。

我想将数据从HashFP.MessageFP复制到ExceptionRow.Message而不覆盖任何数据。 我尝试这样:

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

结果:

消息9002,级别17,状态4,行1数据库'LogStats'的事务日志已满。 要找出为什么无法重用日志中的空间的原因,请参阅sys.databases中的log_reuse_wait_desc列

我尝试了这个:

SELECT name,log_reuse_wait_desc FROM sys.databases

从结果

tempdb   ACTIVE_TRANSACTION
LogStats ACTIVE_TRANSACTION

我如何才能中止那些活动的事务,以便任务能够成功完成?

我如何才能中止那些活动的事务,以便任务能够成功完成?

您不能,因为它是UPDATE FROM事务。
您可以增加日志文件的最大大小:

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

或者,您可以尝试执行以下操作:

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

如果数据库具有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

要么

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

由于您将使用HashFP.MessageFP在null时更新ExceptionRow.Message因此我们无需理会HashFP.MessageFP是否包含null。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM