Insert multiple values into a table

Asked

Viewed 1,662 times

2

How do I insert several values into a table, but into a single one query of sql?

  • 2

    What would be the SGDB?

  • 1

    DBMS stands for Database Management System just in case you don’t know :/ for example Mysql is a DBMS

2 answers

8

SQL Server, Postgresql, Mariadb, DB2 and Mysql:

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

Examples:

Oracle:

You can use the INSERT ALL:

INSERT ALL
   INTO TESTE VALUES (1,'ABACAXI')
   INTO TESTE VALUES (2,'BATATA')
SELECT 1 FROM DUAL;

See working on SQL Fiddle.

Mongodb

db.teste.insert([
    {"id": 1, nome: "ABACAXI"},
    {"id": 2, nome: "BATATA"}
]);

As did not inform the SGDB put the main ones, if not the case, I update later.

  • 1

    By the way, mariaDB, postgres and nosql :)

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.