The sum of all SQL Server matter that exist,
and the space in which all SQL Server events
occur or could occur.
Welcome to SQL Server Universe.com Sign in | Join | Help
in
Home SS SLUG Forums Articles Photos Downloads

How can i generate Incremented Linear Number T-SQL

Last post 05-26-2008, 18:24 by G.R.Preethiviraj Kulasingham. 1 replies.
Sort Posts: Previous
  • How can i generate Incremented Linear Number T-SQL

     05-26-2008, 15:51

    • Joined on 05-16-2008
    • Posts 10
    • Points 0
    • Top 25 Contributor

    hi all

    i wants to generate linear sequence number  like 1,2,3,.............1000000

    ,are there any function like  NEWID() ( this return unique guide, i want to get integer)

     

    thanks 

    IndikaD 

  • Re: How can i generate Incremented Linear Number T-SQL

     05-26-2008, 18:24

    • Joined on 04-02-2007
    • Colombo
    • Posts 28
    • Points 0
    • Top 10 Contributor
      Male

    Hi Indika,

    If you are inserting into a new table using SELECT.. INTO, you can think of using IDENTITY function as part of select statement.

    the syntax for it is like this:

    SELECT IDENTITY(int, 1,1) AS ID_Num
    INTO NewTable
    FROM OldTable

    I am thinking whether you want to generate a sequence number at database level. If that is what you want,  there is no direct method for it. But you can have a workaround.  Keep a table with identity column and insert each time you want to generate it.

    CREATE TABLE dbo.Sequence (SequenceID int IDENTITY(1,1) )

    GO

    INSERT INTO dbo.Sequence Default Values

    SELECT SCOPE_IDENTITY()

    You a have a lot of problems with this approach. Firstly you can't use this routine like a function. (you can think of ROW_NUMBER() Ranking function for it, but in a multi user environment you may face duplicates on that approach as well).  You you want to use this against a heavily used environment,  you may see the table as a bottleneck. As this table grows it may eat soem space of your database.  You can  have a job to remove old data periodically.

     

     

     

     

     


    G.R.Preethiviraj Kulasingham MCITP: DBA
    http://preethiviraj.blogspot.com/
    Plan Your Work and Work Your Plan!
View as RSS news feed in XML

(Best viewed with a resolution of more than 1024 * 768)

Powered by Community Server, by Telligent Systems