Skip to content

Multi Row Insert

Wouter Demuynck edited this page May 14, 2016 · 1 revision

With Popsql you can generate SQL statements you can used to perform multi-row inserts into a table. You can do this by adding multiple calls to the Values method on the INSERT statement you're building.
For example, if we were to insert multiple new users to the database in the same SQL statement, you can write this:

var sql = Sql
    .Insert()
    .Into("User", "UserName", "Email")
    .Values("JSmith", "jsmith@example.com")
    .Values("JDoe", "jdoe@example.com")
    .Values("LDopa", "ldopa@example.com")
    .ToSql();

Which will generate the following SQL text:

INSERT INTO [User] ([UserName], [Email])
VALUES 
    ('JSmith', 'jsmith@example.com'), 
    ('JDoe', 'jdoe@example.com'), 
    ('LDopa', 'ldopa@example.com')

⚠️ Not all database servers support this syntax.

Clone this wiki locally