使用sql server将数据从一个表复制到另一个表

时间:2015-02-25 01:41:28

标签: sql sql-server

我需要编写一个查询,将数据从一个表(CUSTOMER_TABLE with 130 columns)复制到(FAIL_CUSTOMER_TABLE with 132 columns)中的另一个表sql server

FAIL_CUSTOMER_TABLE有两个额外的列,但都声明了default个值。

我在下面的查询中遇到异常。任何人都可以帮助我编写查询,而无需列出查询中的所有130列。

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

错误消息

  

表中标识列的显式值   ' dbo.FAIL_CUSTOMER_TABLE'只能在列列表中指定   used和IDENTITY_INSERT为ON。

1 个答案:

答案 0 :(得分:2)

您错过了在SET IDENTITY_INSERT dbo.XML_FAIL_RECORDS ON

之前添加insert

此外,您需要在设置column list

时提及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