2012-11-07 42 views
0

我有以下代碼:我可以在SQL Server 2008的遊標中使用IDENTITY_INSERT嗎?

declare c cursor for 
      select 
       id_user_V3 
       ,device_type 
       ,4 
       ,notfication_mode 
       ,t1.device_token 
       ,registration_date 
       ,rank 
       ,1 
      from audiv2.dbo.mya_webservice_device_token t1 
       join migr.asoc_V2_glb_user t2 on t1.user_id = t2.id_user_V2 

     open c 
     fetch next from c into @id_user_v3,@type,@application_id,@notiffication,@device_tooken,@creation_date,@rang,@status 
     while @@FETCH_STATUS=0 
      begin 

       set @device_id = (ISNULL((SELECT MAX(id_device) FROM usr_device)+1,1)) 

       SET IDENTITY_INSERT usr_device ON 

       insert into usr_device values 
       (
        @device_id 
        ,@id_user_v3 
        ,@type 
        ,@application_id 
        ,@notiffication 
        ,@device_tooken 
        ,@creation_date 
        ,@rang 
        ,@status 
       ) 
       SET IDENTITY_INSERT usr_device OFF 

       fetch next from c into @id_user_v3,@type,@application_id,@notiffication,@device_tooken,@creation_date,@rang,@status 
      end 

     close c 
     deallocate c 

我得到的錯誤是:

當使用列列表只能指定表「usr_device」的標識列一個明確的價值, IDENTITY_INSERT已打開。

我該如何解決這個問題?

在此先感謝

+5

通過再次讀取錯誤消息或搜索此錯誤消息?有*兩個*條件需要插入一個身份值。 「使用列列表且IDENTITY_INSERT爲ON」。那麼,你已經打開了'IDENTITY_INSERT' ... –

+0

@Damien_The_Unbeliever我插入表 – Gabrielle

+1

之前把IDENTITY_INSERT usr_device打開,但正如錯誤消息所說,並且當我突出顯示,有**兩個**條件你必須符合。你遇到過其中之一(打開'IDENTITY_INSERT') - 你*也必須滿足另一個 - 使用列列表。 –

回答

0

您需要

插入明確地提及的所有列名的INSERT子句中到usr_device(COL1,COL2,......)值

相關問題