Consider one table tbl1 with ID and Name.
A table-valued parameter params with ID.
I need to duplicate the rows in tbl1 which have the ID contained in params.
I am trying to insert like this
insert into tbl1(Name,somevalues..)
select tbl1.Name, tbl1.somevalues..
from @params p
inner join tbl1 on tbl1.ID=p.ID -- here ID is identity (PK).
But after insertion, it is found that the order of insertion is different. If I am running the select query alone, it will display the ID based on @params (TVP). But the same query when I used after insert will perform in a different way.
order in TVP / Select Query – A
order when insert using Select Query – B
A B
370294 370294
368702 368702
369157 368504
368914 368505
368505 368914
368504 369157
368508 368506
368506 368507
368507 368508
368912 368637
368637 368638
368638 368639
368639 368912
368915 368915
368641 368641
There some ID’s which are different in order.
What will be the issue for different order?
How can I insert values in the same order as in my TVP ?
Thanks in advance.
Always: ORDER is arbitrary without an ORDER BY.
This means that even if you did use ORDER BY for the INSERT, then a later SELECT isn’t guaranteed to return with this order unless you use ORDER BY on the SELECT.
In other words: there is no implied or natural order to a table or view.
You can only have a defined order with ORDER BY
Edit
However. it should be noted that INNER JOINs are both commutative and associative. That is, you get the same results. You’re asking about ORDER which I answered above
For more, see