Results 1 to 2 of 2
  1. #1
    manik's Avatar
    manik is online now Om Shanti!
    Join Date
    Apr 2008
    Location
    Boston, USA
    Posts
    13,175
    Thanks
    744
    Thanked 699 Times in 550 Posts
    Blog Entries
    4
    Feedback Score
    4 (100%)

    Default Insert Max int Type Primary Column Value + 1 - Microsoft Sql

    How to Insert Max int Type Primary Column Value + 1 - Microsoft Sql

    If you have a primary column of type int, you need to insert unique values for it, best approach is set the column IS IDENTITY type true with identity seed 1. That way you wont have to worry about it.

    However in some cases you might not be able to do that. Recently i had to work for a large health provider in MA area. They gave me an access to the db without proper privileges, which doesn't allow me set the column IS IDENTITY true. Since i am in rush to finish i have to come up with a way i could insert unique incremental values for the int primary key column on a table.

    insert into MyTable(tId)
    Select IsNull((Select Max(IsNull(tId,0))+1 from MyTable),1)

    Note that I have to wrap the inner subquery in a IsNull for the case when there is no rows in table.
    Free Classified Ads & BUSINESS/PROFESSIONAL SOCIAL NETWORK


  2. #2
    mandy is offline Junior
    Join Date
    Apr 2011
    Posts
    332
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Feedback Score
    0

Similar Threads

  1. Set Identity Insert On Off - Microsoft SQL
    By manik in forum Databases
    Replies: 3
    Last Post: 04-05-2011, 02:08 AM
  2. Get Column Names of Table - Microsoft Sql
    By manik in forum Databases
    Replies: 0
    Last Post: 03-02-2011, 05:08 PM
  3. Different topics for my primary domain
    By jaabirdon78 in forum Search Engine Optimization
    Replies: 1
    Last Post: 02-26-2011, 12:03 AM
  4. Replies: 0
    Last Post: 01-26-2011, 01:24 PM
  5. Replies: 0
    Last Post: 12-21-2010, 05:42 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Partners: BOSTON WEB DEVELOPER, LLC   |   WEBCOSMO CLASSIFIEDS