How to do a "for each" in SQLite

2

I have a table A with n records. For each record in this table, I want to get the information from a x field to make a select and, from the result data of this select , make a insert on a table B. Only that I need to do this for all n A records.

How do I do this in SQLite? Is there something similar to a "for each" for this situation? Or can you just do this operation for an application?

    
asked by anonymous 12.06.2018 / 20:34

1 answer

4

SQLite is a database and not a programming language, so the declarative language you have in it allows something similar that is the select you already know. for each is typically imperative.

You can extend SQLite to accept other things. It is open source and even has some facilities to include customizations. Then you could create a sort of stored procedure or SQL extension contained in it. Of course, it's not something simple to do. It could be in C, a language of its own, a change of standard SQL or other language, such as Lua, C #, or other already known, each with its advantages and disadvantages.

So it's easier to query easily and then manipulate the application. That's why understanding everything you can do in a query can help you filter out just what you want. I see lots of bad queries on codes out there, including from experienced people. In part because the SQL query model does not help, so many go to NoSQL, but often for the wrong reasons.

Having said all this it is possible to make a insert based on a select , that is, the data source of insert can be the result of a select . As the question does not say what it needs, I could not help it anymore. Example:

INSERT INTO b (meuX) VALUES (x)
    SELECT x FROM a;
    
12.06.2018 / 21:06