简体   繁体   中英

Copy a column from one table into another table

I have tried this with ALTER TABLE to create the column followed by INSERT INTO . This kind of works, except each subsequent column starts after the previous column has ended. I guess this is how insert into works, so is there a workaround or another query I can build?

I have been trying with updates but its not working out. For reference, these were the alter/insert queries i used.

SQL = "ALTER TABLE [results] ADD COLUMN [" & fld.Name & "_result] TEXT(25)"
db.Execute SQL
SQL = "INSERT INTO [results] ([" & fld.Name & "_result]) SELECT [Result] As 
    [" & fld.Name & "_result] FROM [newtable]"
db.Execute SQL

Your insert statement assumes that the results table has only one column that you need to insert data into. This is unlikely to be true, if the table already had other columns before you executed the ADD COLUMN.

You will need to keep track of the columns in the results table, and provide data (or a default value) for each column.

It is rather unusual to expand a table's structure from inside an application. What are you trying to accomplish? Are you sure you can't accomplish it better by defining fixed tables and then adding data from your application?

UPDATE

Okay, I think I understand what you're describing. On the first iteration, the ALTER TABLE creates the first column. The INSERT adds a bunch of rows that have data in this first column.

On the second interation, the ALTER TABLE creates a second column. The INSERT creates a whole bunch of new rows, but only the second column is populated. The first column is all NULL because you didn't provide values for it. And so on and so forth for the third and subsequent iterations.

If your actual intention is to duplicate the source table and its data, then you should create your results table in a single pass. You know the column structure, right? Use a CREATE TABLE statement. Then write a single INSERT statement somewhat like the following:

INSERT INTO [results] 
([field1_result], [field2_result], [field3_result]) 
SELECT [Result] As 
[field1_result, [field2_result], [field3_result]] 
FROM [newtable]

Is this what you have in mind?

Before you enter into the loop create your [results] table as

SQL = "CREATE TABLE [results] SELECT [primary_key] FROM [newtable]"
db.Execute SQL

Then at every iteration of the loop execute

SQL = "ALTER TABLE [results] ADD COLUMN [" & fld.Name & "_result] TEXT(25)"
db.Execute SQL

SQL = "UPDATE [results] SET r.[" & fld.Name & "_result] = n.[Result] " & 
      "FROM [results] r, [newtable] n " &
      "WHERE r.[primary_key] = n.[primary_key]"
db.Execute SQL

So, if you had your [newtable] at its first two iterations like

[primary_key] [Results]    [primary_key] [Results]
     1           A              1           D
     2           B              2           E
     3           C              3           F

Your [results] table (after the above two iterations) would look like

[primary_key] [fld1_result] [fld2_result]
     1             A             D
     2             B             E
     3             C             F

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM