Forum: Ruby on Rails SQL Server AR adapter and BULK INSERT

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2007-07-07 01:57

If you don't know about how to bulk import data into SQL Server with the
BULK INSERT statement, you can stop reading.

Rails 1.1.6
SQL Server Adapter (ODBC mode)

I have a situation where I am performing a BULK INSERT followed by a
DELETE statement.

I'm using the same connection from the same model object to do both

It seems like if I perform a BULK INSERT such that the # of rows that
I'm inserting > the batch size specified for the BULK INSERT, then my
DELETE fails with the dreaded: "Connection is busy with results for
another hstmt" ODBC error.

However, if the # of rows is smaller than the BULK INSERT batch size,
the DELETE goes through fine.

Does anyone have any insight into this?  I don't expect that anyone will
have any experience with this, but thought I might try for a long shot.

Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2007-07-18 23:11
Some more information:

If I set the BATCHSIZE for the BULK INSERT to just (# of rows to be
inserted) + 1, then I can immediately turn around and do the DELETE with
no problem.

But if BATCHSIZE <= (# of rows to be inserted), then the DELETE fails
and says that the connection is busy with results of any statement.
That statement appears to be: "SELECT @@ROWCOUNT AS AffectedRows"

I've read the relevant code in the SQL Server adapter, and that query is
only supposed to happen for regular INSERTS and DELETEs.  I cannot
figure out how "SELECT @@ROWCOUNT AS AffectedRows" is getting initiated
due to my BULK INSERT.  My best guess is that the BULK INSERT gets
turned into multiple regular INSERT statements which then behave as

Anyhow, my workaround is to always dynamically set the BATCHSIZE for the
BULK INSERT to be the (# of rows to be inserted) + 1, but of course,
this defeats the memory saving purpose of the BATCHSIZE parameter in the
first place.

Maybe another patch for the 'ol SQL Server adapter (I have 3 still to

This topic is locked and can not be replied to.