Insert multiple values into one table

2

How do I insert multiple values into a table, but in a single query of sql ?

    
asked by anonymous 14.03.2018 / 13:45

2 answers

7

SQL Server, PostgreSQL, MariaDB, DB2 and MySQL:

INSERT INTO TESTE VALUES (1,'ABACAXI'), (2,'BATATA');

Examples:

Oracle:

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 .

MongoDB

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.

    
14.03.2018 / 14:32
0

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
    
14.03.2018 / 16:06