I would like to fill a table with the results of a query on existing table. How can I do that?
If the destination table does not exist, you can create it first with a CREATE TABLE statement, and then copy rows into it with INSERT ... SELECT . A second option is to use CREATE TABLE ... SELECT , which creates the destination table directly from the result of the SELECT .
From the Query Designer menu, point to Change Type, and then click Insert Results. In the Choose Target Table for Insert Results Dialog Box, select the table to copy rows to (the destination table).
After a database has been created, there are two ways of populating the tables – either from existing data or through the use of the user applications developed for the database.
Populate one table using another table. You can populate the data into a table through the select statement over another table; provided the other table has a set of fields, which are required to populate the first table.
(You don't need to match the table schemas)
INSERT tbl_name (col1, col2)
SELECT value1, value2
FROM othertable
See the reference for INSERT ... SELECT Syntax
insert into table_name ...
select * from table_name where ....
The target table and the source query must match in number of columns and datatypes
See this link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With