title: Can I use IDENTITY_INSERT in a cursor on SQL Server 2008?
body: <p>I have the following code: </p>
  
  <pre><code> 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 
  </code></pre>
  
  <p>The error I get is :</p>
  
  <blockquote>
    <p>An explicit value for the identity column in table 'usr_device' can only be specified when a column list is used and IDENTITY_INSERT is ON.</p>
  </blockquote>
  
  <p>How can I solve this?</p>
  
  <p>Thanks in advance</p>