简体   繁体   English

表中身份列的显式值错误?

[英]Explicit value for the identity column in table error?

I get the below error message, I have debugged both identity inserts and they execute with no problems. 我收到以下错误消息,我已经调试了两个身份插入,并且它们执行都没有问题。 Has anyone had trouble with this error before and does anyone know how to fix this? 以前有人遇到过此错误吗?有人知道如何解决此问题吗?

Msg 8101, Level 16, State 1, Line 1 信息8101,第16级,状态1,第1行
An explicit value for the identity column in table 'c365online_script1.dbo.tProperty' can only be specified when a column list is used and IDENTITY_INSERT is ON. 仅当使用列列表且IDENTITY_INSERT为ON时,才能为表'c365online_script1.dbo.tProperty'中的identity列指定一个显式值。

Code: 码:

declare @Source_Database_Name varchar(255) = 'Production2';
declare @Destination_Database_Name varchar(255) = 'c365online_script1';

declare @Company_Id int = 1 --declare a companyid 

CREATE TABLE #CompanyID (ID bigint)

INSERT INTO #CompanyID(ID)
    VALUES('15')

-- Copy over company records from tCompanytable

--FIRST CURSOR LOOP THROUGH THIS TABLE
CREATE TABLE #TableList (
    processorder int,
    tablename NVARCHAR(100)
    )
INSERT INTO #TableList (processorder, tablename )
VALUES
(1, 'tProperty');

DECLARE @Counter INT = 0  -- counting variable

----------- Cursor specific code starts here ------------
-- company cursor
declare copyCompanyDataCursor CURSOR fast_forward FOR
SELECT ID from #CompanyID;

open copyCompanyDataCursor
fetch next from copyCompanyDataCursor into @Company_Id;

WHILE @@FETCH_STATUS = 0
    BEGIN
        declare @processorder int;
        declare @tablename varchar(500);
        -- table cursor

        declare copyTableDataCursor CURSOR fast_forward FOR
        SELECT processorder,tablename from #TableList order by processorder;

        open copyTableDataCursor
        fetch next from copyTableDataCursor into @processorder, @tablename;

        while @@FETCH_STATUS = 0
        BEGIN
            SET IDENTITY_INSERT [c365online_script1].[dbo].[tCompany] ON

            -- Does the table have a companyID column? if statement checking for company id
            IF EXISTS(SELECT * FROM Production2.INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME='CompanyID' and TABLE_NAME=@tablename)
            BEGIN
                if @Counter <= 0 -- make sure loop executes only once.
                BEGIN                   
                declare @debug varchar(max)             
                SET @debug = 'INSERT INTO ' + @Destination_Database_Name + '.dbo.' + @tablename + ' SELECT * FROM ' + @Source_Database_Name + '.dbo.' + @tablename + ' WHERE ' + @Source_Database_Name + '.dbo.' + @tablename + '.CompanyID = ' + CAST(@Company_Id as varchar(10))
                print @debug
                set @Counter = 1
                EXEC(@debug)
                --EXEC('INSERT INTO ' + @Destination_Database_Name + '.dbo.' + @tablename + ' SELECT * FROM ' + @Source_Database_Name + '.dbo.' + @tablename + ' WHERE ' + @Source_Database_Name + '.dbo.' + @tablename + '.CompanyID = ' + @Company_Id )           
                END             
            END
            ELSE
            BEGIN 
                    Print 'No'
            END 
            -- if yes then copy data based on companyID in cursor  
            -- if no check if this is the first time through company loop and copy all data
            -- if @firstloop company exists look at information schema

                    --EXEC('INSERT INTO ' + @Destination_Database_Name + '.dbo.' + @tablename + ' SELECT * FROM ' + @Source_Database_Name + '.dbo.' + @tablename )
                    -- company logic


            SET IDENTITY_INSERT [c365online_script1].[dbo].[tCompany] OFF

            FETCH NEXT FROM copyTableDataCursor into @processorder,@tablename;
        END

        close copyTableDataCursor;

        Deallocate copyTableDataCursor;

--INSERT INTO c365online_script1.dbo.tCompany
--SELECT *
--FROM production2.tCompany
--WHERE ISNULL(CompanyID, 0) = 0  -- copy all data where id is equal to zero
--@Destination_Database_Name

--      
        --EXEC(INSERT  + @Destination_Database_Name + '.dbo.' + @tablename + ' SELECT * FROM ' + @Source_Database_Name + '.dbo.' + @tablename + ' WHERE ' + @Source_Database_Name + '.dbo.' + @tablename + '.CompanyID = ' + @Company_Id + ')')     
        --SET @firstLoop = false;
        FETCH NEXT FROM copyCompanyDataCursor into @Company_Id;
    END

CLOSE copyCompanyDataCursor;
DEALLOCATE copyCompanyDataCursor;


--Cleanup
DROP TABLE #CompanyID
DROP TABLE #TableList

Well, the error says it all: 好吧,错误说明了一切:

An explicit value for the identity column in table 'c365online_script1.dbo.tProperty' can only be specified when a column list is used and IDENTITY_INSERT is ON. 当使用列列表且IDENTITY_INSERT为ON 时,才能为表'c365online_script1.dbo.tProperty'中的identity列指定一个显式值。

So your INSERT statement MUST use a column list! 因此,您的INSERT语句必须使用列列表!

Use 采用

INSERT INTO dbo.Table(col1, col2, ...., colN) VALUES(Val1, val2, ...., ValN)

and not just 而不仅仅是

INSERT INTO dbo.Table   VALUES(Val1, val2, ...., ValN)
                     ^^^^  no column list defined!! 

then it'll work! 这样就可以了!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架错误:无法为表中的标识列插入显式值 - Entity Framework error: Cannot insert explicit value for identity column in table 无法为表中的标识列插入显式值 - Cannot insert explicit value for identity column in table 无法添加表行,因为“当 IDENTITY_INSERT 设置为 OFF 时,无法为表‘用户’中的标识列插入显式值”错误 - Cannot add a table row because of "Cannot insert explicit value for identity column in table 'Users' when IDENTITY_INSERT is set to OFF" error 错误错误:当IDENTITY_INSERT设置为OFF时,无法为表&#39;Table1&#39;中的标识列插入显式值 - WRONG ERROR: Cannot insert explicit value for identity column in table 'Table1' when IDENTITY_INSERT is set to OFF 复制-必须为表中的标识列指定显式值 - Replication - Explicit value must be specified for identity column in table 创建播放器时,无法为表中的身份列插入显式值 - Cannot insert explicit value for identity column in table when player is created 我该如何解决“表中标识列的显式值”? - How can i solve “An explicit value for the identity column in table”? 仅当使用列列表且IDENTITY_INSERT为ON时,才能为表&#39;Table&#39;中的标识列指定一个显式值。 - An explicit value for the identity column in table 'Table' can only be specified when a column list is used and IDENTITY_INSERT is ON RODBC 错误 - 无法为标识列插入显式值 - RODBC Error - Cannot insert explicit value for identity column 只有在使用列列表并且 IDENTITY_INSERT 为 ON 时,才能为表“Fee”中的标识列指定显式值 - An explicit value for the identity column in table 'Fee' can only be specified when a column list is used and IDENTITY_INSERT is ON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM