Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have altered a table to add one column on sql server 2017, now I want to migrate a data from the old table to the new one my challenge now I can't map the columns in both table in my script like on the below script that I am using:
when I run it the values of this column outst60 is moving to the new add column outst80 on the new table what I want the new column on the new table must be null and the columns must be map to each other on the below script.
can someone help me please.

What I have tried:

below is what I have treid
SERT INTO [dbo].[irDataWIP]
           ([yyyy]
           ,[mm]
           ,[totaloutstanding]
           ,[outst30]
           ,[outst60]
           ,[outst80]
           ,[outst90]
           ,[outst120]
           ,[outst150]
           ,[outst180]
           ,[outst210]
           ,[outst2100])
     SELECT TOP (1000) [yyyy]
      ,[mm]
      ,[cnt]
      ,[totaloutstanding]
      ,[outst30]
      ,[outst60]
      ,[outst90]
      ,[outst120]
      ,[outst150]
      ,[outst180]
      ,[outst210]
      ,[outst2100]
  FROM [LiveTest].[dbo].[irDataWIP_Prod19062019]
GO
Posted
Updated 19-Jun-19 23:26pm
Comments
[no name] 20-Jun-19 5:26am    
You miss the column [cnt] in the INSERT list or in other words simply compare the two fieldname lists and you should easely see the problem.

1 solution

Please, read this: SQL Server: SELECT INTO Statement[^]

Quote:
If you find that you want to rename some of the columns within the new table rather than using the original names, you can alias the column names in the SELECT INTO statement.

For example:

SQL
SELECT employee_id AS contact_id, last_name, first_name
INTO contacts
FROM employees
WHERE employee_id < 1000;


In this SELECT INTO example, we don't want the first column in the new contacts table to be called employee_id. It would be more meaningful to rename the first column in the contacts table to contact_id. This is done by aliasing the employee_id column as follows:

SQL
employee_id AS contact_id


SELECT TOP(100) [yyyy]
           ,[mm]
           , NULL AS [cnt]
           ,[totaloutstanding]
           ,[outst30]
           ,[outst60]
           ,NULL AS [outst80]
           ,[outst90]
           ,[outst120]
           ,[outst150]
           ,[outst180]
           ,[outst210]
           ,[outst2100]
 INTO [dbo].[irDataWIP]
 FROM [LiveTest].[dbo].[irDataWIP_Prod19062019]
 
Share this answer
 
v2
Comments
Member 14114251 20-Jun-19 5:38am    
Thanks for your response, however my challenge that I have is the below error
The select list for the INSERT statement contains fewer items than the insert list. The number of SELECT values must match the number of INSERT columns.
Maciej Los 20-Jun-19 5:49am    
The number of columns must be equal. So, you have to change your SELECT statement accordingly.
Member 14114251 20-Jun-19 5:51am    
Thanks yes I changed am sorted now.
Maciej Los 20-Jun-19 5:57am    
You're very welcome. In a meanwhile i had improve my answer.
Cheers!
Maciej

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900