Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. canot update identity column

canot update identity column

Scheduled Pinned Locked Moved Database
helpsharepointdatabaseannouncement
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    future3839
    wrote on last edited by
    #1

    Hi, I am running VS2008 and use SQLExpress. I craete a SP in my database. then when I wanna save it I will get this error : Cannot update identity column 'Code' the above code is my SP

    Create PROCEDURE dbo.SprocInsertUpdatePerson
    (
    @code int,
    @id int,
    @name nvarchar(50),
    @passportNumber nvarchar(50),
    @nationality nvarchar(50),
    @created datetime,
    @creator nvarchar(50)
    )
    AS

    Declare @ReturnValue int
    if (@Code IS NULL) --New Item
    Begin
    	insert into Person
    	(
    		Code,
    		Id,
    		Name,
    		PassportNumber,
    		Nationality,
    		Created,
    		Creator
    	)	
    	values
    	(
    		@code,
    		@id,
    		@name,
    		@passportNumber,
    		@nationality,
    		@created,
    		@creator
    	)
    	
    	Select @ReturnValue = Scope\_Identity()
    	
    End
    
    
    else
    
    Begin --Update Item
    	Update Person  		
    	Set Code = @code,
    	Id = @id,
    	Name = @name,
    	PassportNumber = @passportNumber,
    	Nationality = @nationality,
    	Created = @created,
    	Creator = @creator				
    	where Id = @id
    	
    	Select @ReturnValue = @Id
    End
    
    
    IF (@@ERROR != 0)
    BEGIN
    	RETURN -1
    END
    ELSE
    BEGIN
    	RETURN @ReturnValue
    END
    

    I really appreciate if someone help me. thx a lot

    L N B 3 Replies Last reply
    0
    • F future3839

      Hi, I am running VS2008 and use SQLExpress. I craete a SP in my database. then when I wanna save it I will get this error : Cannot update identity column 'Code' the above code is my SP

      Create PROCEDURE dbo.SprocInsertUpdatePerson
      (
      @code int,
      @id int,
      @name nvarchar(50),
      @passportNumber nvarchar(50),
      @nationality nvarchar(50),
      @created datetime,
      @creator nvarchar(50)
      )
      AS

      Declare @ReturnValue int
      if (@Code IS NULL) --New Item
      Begin
      	insert into Person
      	(
      		Code,
      		Id,
      		Name,
      		PassportNumber,
      		Nationality,
      		Created,
      		Creator
      	)	
      	values
      	(
      		@code,
      		@id,
      		@name,
      		@passportNumber,
      		@nationality,
      		@created,
      		@creator
      	)
      	
      	Select @ReturnValue = Scope\_Identity()
      	
      End
      
      
      else
      
      Begin --Update Item
      	Update Person  		
      	Set Code = @code,
      	Id = @id,
      	Name = @name,
      	PassportNumber = @passportNumber,
      	Nationality = @nationality,
      	Created = @created,
      	Creator = @creator				
      	where Id = @id
      	
      	Select @ReturnValue = @Id
      End
      
      
      IF (@@ERROR != 0)
      BEGIN
      	RETURN -1
      END
      ELSE
      BEGIN
      	RETURN @ReturnValue
      END
      

      I really appreciate if someone help me. thx a lot

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You can remove it from the insert-statement; an IDENTITY[^] is automatically generated when a new record is inserted. Kinda like an autonumber-column in Microsoft Access. The Scope_Identity would then return that newly generated value.

      I are Troll :suss:

      1 Reply Last reply
      0
      • F future3839

        Hi, I am running VS2008 and use SQLExpress. I craete a SP in my database. then when I wanna save it I will get this error : Cannot update identity column 'Code' the above code is my SP

        Create PROCEDURE dbo.SprocInsertUpdatePerson
        (
        @code int,
        @id int,
        @name nvarchar(50),
        @passportNumber nvarchar(50),
        @nationality nvarchar(50),
        @created datetime,
        @creator nvarchar(50)
        )
        AS

        Declare @ReturnValue int
        if (@Code IS NULL) --New Item
        Begin
        	insert into Person
        	(
        		Code,
        		Id,
        		Name,
        		PassportNumber,
        		Nationality,
        		Created,
        		Creator
        	)	
        	values
        	(
        		@code,
        		@id,
        		@name,
        		@passportNumber,
        		@nationality,
        		@created,
        		@creator
        	)
        	
        	Select @ReturnValue = Scope\_Identity()
        	
        End
        
        
        else
        
        Begin --Update Item
        	Update Person  		
        	Set Code = @code,
        	Id = @id,
        	Name = @name,
        	PassportNumber = @passportNumber,
        	Nationality = @nationality,
        	Created = @created,
        	Creator = @creator				
        	where Id = @id
        	
        	Select @ReturnValue = @Id
        End
        
        
        IF (@@ERROR != 0)
        BEGIN
        	RETURN -1
        END
        ELSE
        BEGIN
        	RETURN @ReturnValue
        END
        

        I really appreciate if someone help me. thx a lot

        N Offline
        N Offline
        NeverHeardOfMe
        wrote on last edited by
        #3

        The error message has told you the problem: you cannot update teh identity column. Values for this column are automatically set by the database engine when you add a new record. They are read-only after that, as a unique identifier for that row. So you should not try to set the ID when updating records, just use it in the WHERE clause to specify which row to update.

        1 Reply Last reply
        0
        • F future3839

          Hi, I am running VS2008 and use SQLExpress. I craete a SP in my database. then when I wanna save it I will get this error : Cannot update identity column 'Code' the above code is my SP

          Create PROCEDURE dbo.SprocInsertUpdatePerson
          (
          @code int,
          @id int,
          @name nvarchar(50),
          @passportNumber nvarchar(50),
          @nationality nvarchar(50),
          @created datetime,
          @creator nvarchar(50)
          )
          AS

          Declare @ReturnValue int
          if (@Code IS NULL) --New Item
          Begin
          	insert into Person
          	(
          		Code,
          		Id,
          		Name,
          		PassportNumber,
          		Nationality,
          		Created,
          		Creator
          	)	
          	values
          	(
          		@code,
          		@id,
          		@name,
          		@passportNumber,
          		@nationality,
          		@created,
          		@creator
          	)
          	
          	Select @ReturnValue = Scope\_Identity()
          	
          End
          
          
          else
          
          Begin --Update Item
          	Update Person  		
          	Set Code = @code,
          	Id = @id,
          	Name = @name,
          	PassportNumber = @passportNumber,
          	Nationality = @nationality,
          	Created = @created,
          	Creator = @creator				
          	where Id = @id
          	
          	Select @ReturnValue = @Id
          End
          
          
          IF (@@ERROR != 0)
          BEGIN
          	RETURN -1
          END
          ELSE
          BEGIN
          	RETURN @ReturnValue
          END
          

          I really appreciate if someone help me. thx a lot

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #4

          Have you tried

          SET IDENTITY_INSERT table ON

          I use it when migrating data from an old database into an empty database. But normally, IDENTITY should be set by the database, not by your code.

          F 1 Reply Last reply
          0
          • B Bernhard Hiller

            Have you tried

            SET IDENTITY_INSERT table ON

            I use it when migrating data from an old database into an empty database. But normally, IDENTITY should be set by the database, not by your code.

            F Offline
            F Offline
            future3839
            wrote on last edited by
            #5

            Hi, thanks for your answer; I will get the solution in the last post.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups