简体   繁体   中英

Copy data from one table to another table using sql server

I need to write a query to copy data from one table (CUSTOMER_TABLE with 130 columns) to another table (FAIL_CUSTOMER_TABLE with 132 columns) in sql server .

FAIL_CUSTOMER_TABLE has two extra columns but both have default values declared.

I am getting below exception for the below query. Can any one help me in writing the query with out listing all 130 columns in the query.

INSERT into dbo.FAIL_CUSTOMER_TABLE
select *,'N' AS 'FIXED', CURRENT_TIMESTAMP AS 'DATE' 
from dbo.CUSTOMER_TABLE 
where LTRIM(RTRIM(CustNumber)) !='' 
and Processed = 'N' 
order by ROWID asc 

SET IDENTITY_INSERT dbo.XML_FAIL_RECORDS OFF

Error message

explicit value for the identity column in table 'dbo.FAIL_CUSTOMER_TABLE' can only be specified when a column list is used and IDENTITY_INSERT is ON.

You missed to add SET IDENTITY_INSERT dbo.XML_FAIL_RECORDS ON before the insert

Also you need to mention column list while setting SET IDENTITY_INSERT ON

SET IDENTITY_INSERT dbo.XML_FAIL_RECORDS ON --Missed in your query

INSERT into dbo.FAIL_CUSTOMER_TABLE(Identity_Col,col1,col2...)
select Identity_Col,col1,col2,....,'N' AS 'FIXED', CURRENT_TIMESTAMP AS 'DATE' 
from dbo.CUSTOMER_TABLE 
where LTRIM(RTRIM(CustNumber)) !='' 
and Processed = 'N' 
order by ROWID asc 

SET IDENTITY_INSERT dbo.XML_FAIL_RECORDS OFF

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