Search code examples
sqlvb6sql-insert

Insert values from a list in a single SQL statement


I have a list with values in it. I want to insert these values in a SQL table using a single INSERT statement.

Example: Say, there is a list with names (the size of the list is not constant). There is a STUDENTS table with NAME column. I want to insert in the STUDENTS table the names from the list using a single INSERT statement.

Right now I loop through the list and insert the value in the table. That means, the number of insert statements is equal to the length of the list.

List<String> Name;
foreach (String s in Name)
{
  INSERT INTO STUDENTS (NAME) VALUES (s)
}

Is there a way I can accomplish this in a single SQL INSERT statement? Any help is much appreciated.


Solution

  • In MySql you can do it like this

    INSERT INTO tbl (f1,f2) VALUES(1,2),(3,4),(5,6)...(55,77);
    

    For mssql dialect all the same