patternsqlModerate
order of inserting record is mismatching
Viewed 0 times
insertingmismatchingrecordorder
Problem
Please check the below table structure and records to be inserted in that table.
After inserting all records, when executing a simple select statement on this table like
identity column ID is not coming in proper order or records are inserted randomly. (Check the
Why this is happening?
CREATE TABLE [tabGeneraltable](
id int identity,
[codGenLedger] [uniqueidentifier] NOT NULL,
[codInvoice] [numeric](18, 0) NULL,
[accountValue] [numeric](18, 2) NULL,
[articleValue] [nvarchar](50) NULL,
[codFinAccount] [int] NULL,
[documentNbr] [nvarchar](50) NULL,
[valueDate] [datetime] NULL,
[insertDate] [datetime] NULL,
CONSTRAINT [PK_tabGeneralLedger] PRIMARY KEY CLUSTERED([codGenLedger] ASC)
)
insert into [tabGeneraltable]([codGenLedger],[codInvoice], [codFinAccount],
[accountValue],[insertDate])
select NEWID(),1,11,232,getdate()
union all
select NEWID(),10,45,214,getdate()
union all
select NEWID(),9,425,410,getdate()
union all
select NEWID(),14,475,356,getdate()After inserting all records, when executing a simple select statement on this table like
select * from tabGeneraltableidentity column ID is not coming in proper order or records are inserted randomly. (Check the
insertDate column value if you insert all records separately.) Why this is happening?
Solution
If you don't specify an order by clause your RDBMS is free to return the results as it chooses. Usually it will return the results in the order it can retrieve the records the fastest.
Since you have the column
Your RDBMS will most likely do a clustered index scan in your example and return your results ordered by the clustered index.
See this sqlfiddle for a sql server example.
That is actually sorted by your uniqueidentifier, it doesn't look like it when you look at it alphabetically but it is sorted by the hex representation of the binary data.
See here for a complete explanation.
In fact, the data is sorted; it's just not sorted the way you expect.
SQL Server displays globally unique identifier (GUID) data as a series
of alphanumeric strings, but the uniqueidentifier data type is
equivalent to the binary(16) data type. The GUID value you see is
simply the hexadecimal representation of the binary value. So the data
is sorted by this hex representation, not alphabetically.
Since you have the column
codGenLedger as the clustered key the records will be stored ordered by codGenLedger physically on disk (unless your indexes are fragmented, but they are still read in that order).Your RDBMS will most likely do a clustered index scan in your example and return your results ordered by the clustered index.
See this sqlfiddle for a sql server example.
That is actually sorted by your uniqueidentifier, it doesn't look like it when you look at it alphabetically but it is sorted by the hex representation of the binary data.
See here for a complete explanation.
In fact, the data is sorted; it's just not sorted the way you expect.
SQL Server displays globally unique identifier (GUID) data as a series
of alphanumeric strings, but the uniqueidentifier data type is
equivalent to the binary(16) data type. The GUID value you see is
simply the hexadecimal representation of the binary value. So the data
is sorted by this hex representation, not alphabetically.
Context
StackExchange Database Administrators Q#105118, answer score: 13
Revisions (0)
No revisions yet.