How do I insert multiple values into a table, but in a single query of sql ?
How do I insert multiple values into a table, but in a single query of sql ?
INSERT INTO TESTE VALUES (1,'ABACAXI'), (2,'BATATA');
Examples:
SQL Server
in SQL Fiddle .
MySQL
in SQL Fiddle .
PostgreSQL
in SQL Fiddle .
You can use INSERT ALL
:
INSERT ALL
INTO TESTE VALUES (1,'ABACAXI')
INTO TESTE VALUES (2,'BATATA')
SELECT 1 FROM DUAL;
See working in SQL Fiddle .
db.teste.insert([
{"id": 1, nome: "ABACAXI"},
{"id": 2, nome: "BATATA"}
]);
As I did not inform the SGDB I put the principals, if not the case, I will update later.
With Sql Server, 2 suggestions:
declare @sql nvarchar(max)
set @sql = N'insert into tabela (coluna1, coluna2) values (''coluna1'', ''coluna2'');
insert into tabela (coluna1, coluna2) values (''coluna1'', ''coluna2'');
insert into tabela (coluna1, coluna2) values (''coluna1'', ''coluna2'');'
exec sp_executesql @sql
or
insert into tabela (coluna1, coluna2)
select coluna1, coluna2 from tabela2