Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Inserting Multiple Rows into a Table with a Single Statment

Looking to insert 500 rows into many-to-many table (sample_tag)

Aiming to have 500 new records created in sample_tag linking the first 500 rows from ‘sample’ to associated "tag_id’s" in the ‘tag’ table.

The following code seems to make sense, but triggers an error because the nested SELECT statement returns more than one row.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

INSERT INTO sample_tag (sample_id, tag_id) 
VALUES ((SELECT sample_id from sample where sample_id<=500), 1)

What could be the correct SQL to accomplish this multiple insert?

>Solution :

You can have multiple inserts by not using VALUES keyword. You need to specify same number of columns on your source table.

INSERT INTO sample_tag (sample_id, tag_id) 
SELECT sample_id, tag_id from sample where sample_id<=500
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading