簡體   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