0

If we want to insert multiple values into a table in SQL, we could use the following method:

INSERT INTO MyTable VALUES (1,'a','foo');
INSERT INTO MyTable VALUES (2,'b','bar');
INSERT INTO MyTable VALUES (3,'c','foo');
INSERT INTO MyTable VALUES (4,'d','bar');
INSERT INTO MyTable VALUES (5,'e','foo');

However, this is an RBAR operation which are generally seen as bad:

RBAR: ‘Row By Agonizing Row’

What is RBAR? How can I avoid it?

RBAR vs. Set based programming for SQL

So we would do a single set based INSERT statement:

INSERT INTO MyTable VALUES (1,'a','foo'), (2,'b','bar'), (3,'c','foo'), (4,'d','bar'), (5,'e','foo');

If we are creating a .NET application, and want to carry out the above insert on say a button click, we would use the ADO.NET libraries and would want to do the set based operation above rather than the RBAR one.

However, this doesn't seem easy to do and a Google search brought the following results

Parameterize insert of multiple rows

How should I multiple insert multiple records?

Inserting Multiple Records into SQL Server database using for loop

which seem to recommend executing an INSERT statement in a loop therefore running an INSERT per loop iteration (and therefore an RBAR operation)

I don't understand this, if RBAR is bad, why does there seem to be so little built in support for doing a set based operation in the ADO.NET libraries resulting in the best way of doing this being an RBAR?

SE1986
  • 2,534
  • 1
  • 10
  • 29
  • Have you tried by passing DataTable to Stored Procedure. Please have a look. https://stackoverflow.com/questions/29507629/passing-datatable-to-stored-procedure-as-an-argument – ℛⱥℐℰşℎ Jul 25 '18 at 08:19
  • What database are you working with? Sql Server supports table valued parameters, so you cap execute a single statement passing entire data tables to it, however a lot of other databases (and ado.net implementations) does not. – Zohar Peled Jul 26 '18 at 04:55

0 Answers0