How to insert multiple rows in a single statement?


There are various approaches. For example, if you wish to insert these:

2 two
4 four
5 five

you can use UNIONs (with Firebird 2.0 and above):

INSERT INTO table1 (col1, col2)
SELECT 2, 'two ' FROM RDB$DATABASE UNION ALL
SELECT 4, 'four' FROM RDB$DATABASE UNION ALL
SELECT 5, 'five' FROM RDB$DATABASE;

Please note that datatypes must match, especially in older Firebird versions. In the above example, 'four' and 'five' are varchar(4), while 'two' would evaluate to varchar(3) and you would get an error. To work around this, you can use CAST or, in this case, add anoter space after the word: two, to make it 'two '.

You can also use EXECUTE BLOCK (with Firebird 2.0 and above):

set term ^ ;
EXECUTE BLOCK AS BEGIN
INSERT INTO table1 (col1, col2) VALUES (2, 'two');
INSERT INTO table1 (col1, col2) VALUES (4, 'four');
INSERT INTO table1 (col1, col2) VALUES (5, 'five');
END^


Do you find this FAQ incorrect or incomplete? Please e-mail us what needs to be changed. To ensure quality, each change is checked by our editors (and often tested on live Firebird databases), before it enters the main FAQ database. If you desire so, the changes will be credited to your name. To learn more, visit our add content page.



All contents are copyright © 2007-2024 FirebirdFAQ.org unless otherwise stated in the text.


Links   Firebird   News   FlameRobin   Powered by FB: Home Inventory   Euchre  
Add content   About  

Categories
 Newbies
 SQL
 Installation and setup
 Backup and restore
 Performance
 Security
 Connectivity and API
 HOWTOs
 Errors and error codes
 Miscellaneous