2

This works fine:

insert into Genres (ID, Genre) 
select 1 AS ID, 'Action' AS Genre 
union select 2, 'Adventure' 

...and this works fine:

insert into Movies_Genres (ID, MovieID, GenreID) 
select 1 AS ID, 2 AS MovieID, 1 AS GenreID 
union select 2, 3, 1 

...but when I try to combine the two statements:

insert into Genres (ID, Genre) 
select 1 AS ID, 'Action' AS Genre 
union select 2, 'Adventure' 
insert into Movies_Genres (ID, MovieID, GenreID) 
select 1 AS ID, 2 AS MovieID, 1 AS GenreID 
union select 2, 3, 1 

...it throws an error. I also tried separating the 2 table batches by a semicolon, but also got an error. "An error occured parsing the provided SQL statement." And "syntax error". I have a large amount of inserts to do, so I need to maximize performance, and I have found this approach very robust and fast:

How to insert multiple rows into a SQLite 3 table?

So, what is the proper syntax to batch together a bunch of inserts aimed at different tables?

2
  • Use UNION ALL instead of UNION. Commented Oct 22, 2014 at 16:02
  • How are you delivering the INSERT statements to the sqlite3? If you're doing that using a method that expects a single statement, you will not be able to include both INSERT statements in a single submission. Commented Oct 22, 2014 at 16:51

1 Answer 1

1

One statement cannot insert into multiple table.

Just execute multiple statement. (This will be fast if you use a single transaction.)

Sign up to request clarification or add additional context in comments.

6 Comments

sqlite doesn't have a batch separator (such as semicolon) for multiple statements? All other DB platforms support that, so I just figured sqlite would too.
It has, but the drivers in many languages/libraries do not allow it to prevent injection attacks.
Got it working; just seems strange you can't batch up any insert you want, to any table, in the same batch.
@HerrimanCoder can you tell me please how you got it working i wanna insert into 2 tables
@TeeJay This depends on the language you're using. Ask a new question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.