SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


identity order is not in order


identity order is not in order

Author
Message
Hollyz
Hollyz
SSC-Enthusiastic
SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)

Group: General Forum Members
Points: 163 Visits: 331
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.
Abu Dina
Abu Dina
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1225 Visits: 3323
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

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
Michael Valentine Jones
Michael Valentine Jones
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5692 Visits: 11771
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


Orlando Colamatteo
Orlando Colamatteo
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14853 Visits: 14396
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
GilaMonster
GilaMonster
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86570 Visits: 45245
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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search