473,419 Members | 1,704 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

How to have a batch transaction?

Sri
I am inserting records into a table - around 1 million records. I want
to do the insert 10000 records at a time using TRAN. My insert
statement is very simple:
BEGIN TRAN T1
Insert INTO TABLE A
(ColA, ColB, ColC)
Select B.TBLBCOLA, B.TBLBCOLB, B.TBLBCOLC
FROM tbltable B
WHERE blah balh...etc.

COMMIT TRAN T1
Any hep or link on the syntax will be helpful!
Thanks
Sri

Jul 23 '05 #1
6 3467
Why would you do this? Your insert..select statement is already atomic
and doesn't need a transaction. Besides, you're not checking for
errors and rolling back so it's useless.

Jul 23 '05 #2
Sri
I have to do this in batches because my log ran out of space in tempdb.

Jul 23 '05 #3
You can't do it inside a single transaction, because this won't free-up
any space in the tran log until the whole process has completed.

As Gary says, if you're splitting the insert into multiple batches,
there's no point in placing each one in iots own transaction because
it's atomic.

If you can delete the records from TableB after you've inserted into
TableA, then life is comparatively simple:
declare @counter int
declare @rows_affected int

set rowcount 10000

Insert INTO TABLE A
(ColA, ColB, ColC)
Select B.TBLBCOLA, B.TBLBCOLB, B.TBLBCOLC
FROM tbltable B
WHERE blah balh...etc

select @rows_affected = @@ROWCOUNT

delete Table B
WHERE blah balh...etc

while @rows_affected > 0
begin

Insert INTO TABLE A
(ColA, ColB, ColC)
Select B.TBLBCOLA, B.TBLBCOLB, B.TBLBCOLC
FROM tbltable B
WHERE blah balh...etc

select @rows_affected = @@ROWCOUNT

delete Table B
WHERE blah balh...etc

end

set rowcount 0
This will repeatedly insert 10000 records into table A, then delete the
same from Table B until there are no more records left that match the
WHERE clause. The final iteration of the loop will probably affect <
10000 records.

If you can't delete from tableB, you can use the same principle, but
will have to compare the PK values in TableA with the max equivalent
for those records just inserted into table B, something like:

(Assuming colA is the PK):

set rowcount 10000

Insert INTO TABLE A
(ColA, ColB, ColC)
Select B.TBLBCOLA, B.TBLBCOLB, B.TBLBCOLC
FROM tbltable B
WHERE blah balh...etc

while @@rowcount > 0
begin

select @max_A = max(colA) from TableA

Insert INTO TABLE A
(ColA, ColB, ColC)
Select B.TBLBCOLA, B.TBLBCOLB, B.TBLBCOLC
FROM tbltable B
WHERE blah balh...etc
and ColA > @max_A

end

Jul 23 '05 #4
Doing it in batches is fine, that's not what I was complaining about.
It's using the BEGIN TRAN..COMMIT TRAN, which you don't need unless
you're doing multiple commands and actually checking for errors.
Transactions should be implicit with atomic statements.

Jul 23 '05 #5
Sri
Rather...I thought a batch process might help since this log space ran
out comes up after I fire my query 2 hrs ago.

Jul 23 '05 #6
On 8 Feb 2005 09:34:02 -0800, Sri wrote:
I am inserting records into a table - around 1 million records. I want
to do the insert 10000 records at a time using TRAN. My insert
statement is very simple:
BEGIN TRAN T1
Insert INTO TABLE A
(ColA, ColB, ColC)
Select B.TBLBCOLA, B.TBLBCOLB, B.TBLBCOLC
FROM tbltable B
WHERE blah balh...etc.

COMMIT TRAN T1
Any hep or link on the syntax will be helpful!


Hi Sri,

You'll need something like this generic format:

SET ROWCOUNT 10000
WHILE 1 = 1
BEGIN
INSERT INTO TableA (ColA, ColB, ColC)
SELECT b.ColA, b.ColB, b.ColC
FROM TableB AS b
LEFT JOIN TableA AS a
ON a.ColA = b.ColA
AND a.ColB = b.ColB
-- Replace the above with whichever column (or combination of columns)
-- suffices to uniquely identify exactly one row in the data.
WHERE a.ColA IS NULL
-- Use any column from table A that will never contain NULL
-- The left join with a test for NULL in a non NULLable column
-- is a trick; it has the same effect as a NOT EXISTS subquery,
-- but is often quicker
IF @@ROWCOUNT = 0 BREAK
BACKUP LOG MyDatabase
END
SET ROWCOUNT 0

The "backup log" is necessary - else, the logged events will still be kept
in the log. If you don't need recoverability during this operation (i.e.
it suffices to restore an old full or incremental backup and repeat the
mega insert process), you can also use BACKUP LOG MyDatebase WITH NO_LOG.
If your database is in the simple recovery model, you can omit this step.

If you used BACKUP LOG MyDatabase WITH NO_LOG, then you should take a full
or incremental database backup directly after this process is finished.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Robbie | last post by:
Hi All This is a belter that my little brain can't handle. Basically I have 1 SQL table that contains the following fields: Stock Code Stock Desc Reference Transaction Date
5
by: Tyler Hudson | last post by:
After reading Erland Sommarskog's most enlightening articles on SQL Server 2000's error handling capabilities (http://www.sommarskog.se/error-handling-I.html and...
1
by: JT | last post by:
can someone point me in the right direction for learning more about how to go about sending a batch of sql statements from asp to sql server, and having errors appropriately handled? for...
0
by: Krishna | last post by:
Hi Guys, I saw somuch discussions on this topic but not found the proper answers - but after a struggle I found the following answer - here is the code snippet. Protected Friend Function...
4
by: acantatore | last post by:
Hello! I'm looking for patterns, best practices, examples - reference material - that apply to Batch Processing, that is: Basic job control (start and stop) Job partitioning Parallel...
0
by: Sharath | last post by:
"Inspired" by the huge success of our first two automation fast track batches We are forced to start third fast track automation batch ...
0
by: Sharath | last post by:
We are glad to inform you that "Inspired" by the huge success of our first three automation fast track batches We are forced to start fourth fast track automation batch ...
0
by: Sharath | last post by:
We are glad to inform you that "Inspired" by the huge success of our first four automation fast track batches We are forced to start fifth fast track automation batch ...
3
by: John | last post by:
Hi. I have a number of batch jobs that are ran nightly on our Windows 2000 based Oracle 8.1.7 (soon to be 9i) server. I have these designed just right, so the Windows Scheduled Tasks runs them...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.