我试着做这个查询
INSERT INTO dbo.tbl_A_archive
SELECT *
FROM SERVER0031.DB.dbo.tbl_A
但即使在我跑了之后
set identity_insert dbo.tbl_A_archive on
我得到这个错误消息
表'dbo中标识列的显式值。tbl_A_archive'只能在使用列列表且IDENTITY_INSERT为ON时指定。
tbl_A是一个行和宽都很大的表,也就是说它有很多列。我不想手动输入所有的列。我怎样才能让它工作呢?
请确保从中选择记录的表中的列名、数据类型和顺序与目标表完全相同。唯一的区别应该是目标表有一个标识列作为第一列,这在源表中是没有的。
当我执行“INSERT INTO table_Dest SELECT * FROM table_source_linked_server_excel”时,我也遇到了类似的问题。表格有115列。
I had two such tables where I was loading data from Excel (As linked server) into tables in database. In database tables, I had added an identity column called 'id' that was not there in source Excel. For one table the query was running successfully and in another I got the error "An explicit value for the identity column in table can only be specified when a column list is used and IDENTITY_INSERT is ON SQL Server". This was puzzling as the scenario was exactly same for both the queries. So I investigated into this and what I found was that in the query where I was getting error with INSERT INTO .. SELECT *:
源表中的一些列名被修改,但值是正确的
SELECT *所选择的实际数据列之外还有一些额外的列。我通过使用源Excel表(链接服务器下)上的“脚本表作为>选择>新查询窗口”选项发现了这一点。在Excel中,在最后一列之后有一个隐藏列,尽管它没有任何数据。我在源Excel表格中删除了该列并保存了它。
在做了以上两个更改之后,INSERT INTO…SELECT *运行成功。目标表中的标识列按照预期为每个插入的行生成标识值。
因此,即使目标表可能有源表中没有的标识列,INSERT INTO..如果源和目标中的名称、数据类型和列顺序完全相同,则SELECT *将成功运行。
希望它能帮助到别人。
这应该有用。我刚碰到你的问题
SET IDENTITY_INSERT dbo.tbl_A_archive ON;
INSERT INTO dbo.tbl_A_archive (IdColumn,OtherColumn1,OtherColumn2,...)
SELECT *
FROM SERVER0031.DB.dbo.tbl_A;
SET IDENTITY_INSERT dbo.tbl_A_archive OFF;
不幸的是,似乎您确实需要包含标识列的列列表来插入指定标识的记录。但是,您不必在SELECT中列出列。
正如@Dave Cluderay建议的那样,这将导致一个格式化的列表供您复制和粘贴(如果小于200000字符)。
我添加了USE,因为我要在实例之间切换。
USE PES
SELECT SUBSTRING(
(SELECT ', ' + QUOTENAME(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Provider'
ORDER BY ORDINAL_POSITION
FOR XML path('')),
3,
200000);
两者都可以工作,但如果使用#1仍然会出错,那么就使用#2
1)
SET IDENTITY_INSERT customers ON
GO
insert into dbo.tbl_A_archive(id, ...)
SELECT Id, ...
FROM SERVER0031.DB.dbo.tbl_A
2)
SET IDENTITY_INSERT customers ON
GO
insert into dbo.tbl_A_archive(id, ...)
VALUES(@Id,....)