Results 1 to 6 of 6

Thread: Auto-Incrementing field in SQL Server?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Auto-Incrementing field in SQL Server?

    I am trying to manually add a row of data to a table within SQL Server 2005. The first column, called "MyData_Notes_ID" has the "Is Identity" set to "Yes" (it's also the Primary Key for the table), therefore, I don't include that column in my "INSERT" statement. However, when I execute the statement, it tells me that the "MyData_Notes_ID" field can't be null. What am I doing wrong?

    Thanks,
    Blake

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Auto-Incrementing field in SQL Server?

    What is the Insert statement?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Auto-Incrementing field in SQL Server?

    Here ya go Gary,

    Code:
    Use OA_Runtime
    
    INSERT INTO MyData_Notes
    	(Category,
    	 Title,
    	 Body,
    	 NTLogin,
    	 isCurrentVersion,
    	 isDeleted,
    	 DateCreated,
    	 DateDeleted)
      VALUES
    	('Address Info',
    	 'Wrong Address',
    	 'This is test record #2',
    	 'Z126409',
    	 0,
    	 0,
    	 GetDate(),
    	 GetDate())
    Blake

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Auto-Incrementing field in SQL Server?

    That looks correct. Are you sure that the MyData_Notes_ID is set to IDENTITY?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Auto-Incrementing field in SQL Server?

    Gary,

    I think I found the problem. You can't have more than 1 identity column per table can you? If so, that was my problem...if not, then I don't know! LOL
    Blake

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Auto-Incrementing field in SQL Server?

    Quote Originally Posted by blakemckenna View Post
    Gary,

    I think I found the problem. You can't have more than 1 identity column per table can you? If so, that was my problem...if not, then I don't know! LOL
    That would indeed be the problem. It doesn't make sense to have more than one identity per table. The point of the identity is to uniquely identify each record. Two identities can't do that any better than one, so there's no reason to have two.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width