Roland Bengtsson
Roland Bengtsson

Reputation: 5158

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.

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. But optimizations is not my question.

I want to copy data from HashFP.MessageFP to ExceptionRow.Message without overwriting any data. 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. To find out why space in the log cannot be reused, see the log_reuse_wait_desc column in sys.databases

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 ?

Upvotes: 1

Views: 322

Answers (2)

Igor Borisenko
Igor Borisenko

Reputation: 3866

How can I abort those active transactions so the task can be successful ?

You can't, because it's the UPDATE FROM transaction.
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.

Upvotes: 1

Anto Raja Prakash
Anto Raja Prakash

Reputation: 1388

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..

Upvotes: 0

Related Questions