Professional Web Applications Themes

Howto: Auto increment Primary Key in SQLServer - Microsoft SQL / MS SQL Server

Hi, How do I set a field in a table in a SQLServer database so that it increments automatically everytime I do an insert. For example I have a table with the following fields ID Primary Key int TYPE int VALUE string I want to be able to just insert into this table the following TYPE 101 VALUE 'Temp' TYPE 102 VALUE 'Real' TYPE 103 VALUE 'Complex' TYPE 104 VALUE 'Other' without having to know the ID....

  1. #1

    Default Howto: Auto increment Primary Key in SQLServer

    Hi,

    How do I set a field in a table in a SQLServer database so that it
    increments automatically everytime I do an insert.

    For example I have a table with the following fields

    ID Primary Key int
    TYPE int
    VALUE string

    I want to be able to just insert into this table the following
    TYPE 101 VALUE 'Temp'
    TYPE 102 VALUE 'Real'
    TYPE 103 VALUE 'Complex'
    TYPE 104 VALUE 'Other'

    without having to know the ID.



    Wali Akthar Guest

  2. #2

    Default Re: Howto: Auto increment Primary Key in SQLServer

    You need to define a column of type IDENTITY and specify a starting value
    and an increment. After this, you do not need to insert into this column. To
    get the current value inserted, you can use the IDENTITY global variable
    in SQL Server. Here is a sample CREATE TABLE statement:
    CREATE TABLE sampleTable
    (
    IDCol INT IDENTITY (1,1)
    )
    --
    HTH,
    SriSamp
    Please reply to the whole group only!
    [url]http://www32.brinkster.com/srisamp[/url]

    "Wali Akthar" <waktharhotmail.com> wrote in message
    news:%23i19Q1VQDHA.2480tk2msftngp13.phx.gbl...
    > Hi,
    >
    > How do I set a field in a table in a SQLServer database so that it
    > increments automatically everytime I do an insert.
    >
    > For example I have a table with the following fields
    >
    > ID Primary Key int
    > TYPE int
    > VALUE string
    >
    > I want to be able to just insert into this table the following
    > TYPE 101 VALUE 'Temp'
    > TYPE 102 VALUE 'Real'
    > TYPE 103 VALUE 'Complex'
    > TYPE 104 VALUE 'Other'
    >
    > without having to know the ID.
    >
    >
    >

    SriSamp Guest

Similar Threads

  1. Auto increment VALUE attribute extension
    By arzo2000 in forum Macromedia Exchange Dreamweaver Extensions
    Replies: 1
    Last Post: November 11th, 05:55 PM
  2. Auto Increment increase by 100 at a time?
    By Gary@garywhittle.co.uk in forum MySQL
    Replies: 2
    Last Post: November 23rd, 04:50 PM
  3. How to Handle Primary Key Auto Increment?
    By tbemcf14 in forum Coldfusion - Advanced Techniques
    Replies: 2
    Last Post: March 22nd, 09:47 PM
  4. id after auto increment from a form
    By Faith in forum ASP
    Replies: 7
    Last Post: August 6th, 05:18 PM
  5. Auto Increment a number field
    By jaycee in forum Microsoft Access
    Replies: 0
    Last Post: July 30th, 11:22 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139