identity order is not in order

  • We have a table in our system that has an identity to one of the field.

    One would think when you would do a regular insert into a table, the rows would come in the order of the identity field.

    For example, if Table A has (col_name varchar(20), col3 identity(1,1),

    the inserted rows should come as:

    select * from TableA

    'Donna',1

    'Jack',2

    'David',3

    'Sam',4

    ....,5

    ,...,6

    ....,7

    ....,8

    ...,9

    ...,10

    'Donna',1

    'Jack',2

    'David',3

    'Sam',4

    ....,5

    ,...,6

    ....,7

    ....,8

    ...,9

    ...,10

    ...,14

    But that is not the case.

    We did regular inserts and the tableA didnot come in the order of the identity.

    'Jeff',11

    'Tom,12

    'Maria,13

    'Donna',1

    'Jack',2

    'David',3

    'Sam',4

    ,...,5

    ....,6

    ....,7

    ....,8

    ...,9

    ...,10

    ...,14

    What would cause this out of order sequencing? Is there anyway to maintain the ordering of the identity column in the right physical order of the table?

    We try to add primary key clustered index to the table and then drop the index. It fixed the ordering, but new inserts will still insert in out of order of the Identity column.

  • Is there a clustered index on the table? If so, which columns does it reference?

    oops just saw the last line of your post. Why no set your ID column as your PK?

    ---------------------------------------------------------

    It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
    David Edwards - Media lens[/url]

    Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
    Howard Zinn

  • The only way to ensure that a query returns results in the order that you want is to have an ORDER BY.

    select * from TableA order by col3

  • Hollyz (2/11/2013)


    What would cause this out of order sequencing? Is there anyway to maintain the ordering of the identity column in the right physical order of the table?

    What you see when you select from the table and what is happening internally with respect to storage are two different things. If you want your data logically stored in the order of the IDENTITY column then make the IDENTITY column the clustered index key. Note that the clustered index key and the primary key do not have to be the same, i.e. you can have a nonclustered primary key.

    We try to add primary key clustered index to the table and then drop the index. It fixed the ordering, but new inserts will still insert in out of order of the Identity column.

    As Michael correctly states, the only way to guarantee order when selecting data is to provide an order by.

    If you meant ordering on insert, as in when doing an INSERT...SELECT, you can provide an ORDER BY there too and it will issue the identity values per that ordering although I am not sure why it matters since you must still provide an ORDER BY when selecting data to guarantee order.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Hollyz (2/11/2013)


    One would think when you would do a regular insert into a table, the rows would come in the order of the identity field.

    Table are unordered sets of rows. The insert order has no effect on the order that rows will be returned.

    What would cause this out of order sequencing? Is there anyway to maintain the ordering of the identity column in the right physical order of the table?

    Cause is simple, no order by on the retrieving query. If you want data in a specific order, you must put an order by on the outer query. Otherwise you're telling SQL 'I don't care what order the data comes back in', and SQL will honour your lack of request and return the data in whatever order the last query operator left it.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply