You must Sign In to post a response.
  • Category: SQL Server

    How to add an auto increamental column in sql2008

    Hi Friends,

    In my SQL 2008 database, I want to create a View in which an auto incremant column will be added. i.e. a column by name ID, datatype int and value will be 1,2,3,4......automatically generate. Could any one please help me?

    Best regards
    Bashar
  • #726087
    Hi,

    Prepare a table with auto increment column like below.
    EX:

    create table tab
    (
    ID INT ,
    VAL INT IDENTITY(1,1)
    )



    In the above example val is autoincrement column with increment the each value by 1 and start the value from 1.


    now you can create view of this table.

    EX:

    CREATE VIEW [dbo].[TAB]
    AS
    SELECT ID, VAL
    FROM DBO.TAB


    Using this you can create view to the table with auto incremented column.

    hope this will help you.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #726088
    Use this below Query to add the identity ina table


    Alter table table1 ADD CONSTRAINT column1_def Identity(1,1) FOR Col

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #726109
    hai try also


    Create view Vtest as
    SELECT ROW_NUMBER() OVER ( ORDER BY [Name of the column according to order of which you need Increment column] ASC/DESC) AS [Row Number], [Set of the columns you want to select] FROM TableName

  • #726151
    Hi Basher,

    create Table TableName
    (UserId int Identity(1,1),UserName varchar(90))

    Hope this will help

    Sriram.R

  • #730291
    Hi,


    Create Table Product(pro_id int identity(1,1),proname varchar(90),prodesc varchar(90))



    Create view pro_view
    as
    select pro_id,proname,prodesc from product


    hope this will help


    sriram.r


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.