Skip to content
Mathias Wulff edited this page Dec 15, 2025 · 2 revisions

Keyword INSERT

Syntax:

    INSERT INTO table [(column1, column2...)] [VALUE[S]] valuePair1, valuePair2, ...;
    INSERT INTO table DEFAULT VALUES;
    INSERT INTO table SELECT ...;
    INSERT [IGNORE] INTO table ...;
    INSERT INTO table SET column1 = value1, column2 = value2, ...;

[INSERT [VALUES]](Insert Values)

    alasql('INSERT INTO city (name, population) VALUES ("Moscow",11500000), ("Kyiv",5000000)');
    alasql('INSERT INTO city VALUES {population:4000000, name:"Berlin"}');
    alasql('INSERT INTO city VALUES ?', [data]);
    alasql('INSERT INTO city VALUES ("Copenhagen",1000000)');
    alasql('INSERT INTO city VALUE ("Barcelona",1600000)');
    alasql('INSERT INTO city ("Paris",3500000)');

[INSERT DEFAULT VALUES](Insert Default Values)

    alasql('INSERT INTO city DEFAULT VALUES');

[INSERT SELECT](Insert Select) (equivalent of SELECT INTO)

    alasql('INSERT INTO city SELECT capital AS name FROM country GROUP BY capital;');

INSERT IGNORE

You can use INSERT IGNORE to skip rows that would cause a constraint violation (like duplicate primary keys).

    alasql('INSERT IGNORE INTO city VALUES ("Paris", 3500000)');

INSERT SET

You can use SET syntax to specify column values, similar to UPDATE syntax.

    alasql('INSERT INTO city SET name = "Madrid", population = 3000000');

OUTPUT Clause

You can use the OUTPUT clause to return the inserted data. This is useful when you want to get the generated IDs or default values.

    // Return all inserted columns
    alasql('INSERT INTO city VALUES ("Rome", 2800000) OUTPUT INSERTED.*');

    // Return specific columns
    alasql('INSERT INTO city VALUES ("Rome", 2800000) OUTPUT INSERTED.name, INSERTED.population');

An INSERT statement will return the amount of rows inserted by the statment.


See also: INTO, [SELECT INTO](Select Into)

Clone this wiki locally