Question

I've got a call to a stored procedure, that is basically an INSERT stored procedure. It inserts into Table A, then into Table B with the identity from Table A.

Now, i need to call this stored procedure N amount of times from my application code.

Is there any way i can batch this? At the moment it's doing N round trips to the DB, i would like it to be one.

The only approach i can think of is to pass a the entire list of items across the wire, via an User Defined Table Type.

But the problem with this approach is that i will need a CURSOR in the sproc to loop through each item in order to do the insert (because of the identity field).

Basically, can we batch DbCommand.ExecuteNonQuery() with EF 4.2?

Or can we do it with something like Dapper?

Was it helpful?

Solution

You can keep it like that and in the stored procedure just do a MERGE between your target table and the table parameter. Because you are always coming with new records, the MERGE will enter only on the INSERT branch.

In this case, using MERGE like this is an easy way of doing batch inserts without a cursor.

Also, another way which also avoids the use of a cursor is to use a INSERT from SELECT statement in the SP.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top