I am struggling to comprehend this below,
INSERT INTO Sales.MyOrders(orderid, custid, empid, orderdate)
SELECT NEXT VALUE FOR Sales.SeqOrderIDs OVER(ORDER BY orderid),
custid,
empid,
orderdate
FROM Sales.Orders
WHERE custid = 1;
The book says the
OVER clause with an ORDER BY list to control the order in which the sequence values are assigned to the result rows
If I can get clarification that would be great
From NEXT VALUE FOR (Transact-SQL)
Generates a sequence number from the specified sequence object.
and
Sequence Numbers
A sequence is a user-defined schema-bound object that generates a sequence of numeric values according to the specification with which the sequence was created. The sequence of numeric values is generated in an ascending or descending order at a defined interval and may cycle (repeat) as requested. Sequences, unlike identity columns, are not associated with tables. An application refers to a sequence object to receive its next value. The relationship between sequences and tables is controlled by the application. User applications can reference a sequence object and coordinate the values keys across multiple rows and tables.
This is new functionality in SQL Server 2012 which allows multiple tables to use the same identity sequence, to allow for unique IDs accross different tables.
Have a look at the attached DEMO, you will see the effect of how the autonumber is used accross tables.
The OVER ORDER BY just ensures that the order of the generated sequence is specified.
The above demo should illustrsate how the sequnce is then assigned based on the ORDER of the original IDs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With