I want to create a sequence of numbers in SQL Server that will have a minimum value and maximum value. I want to cycle if the number reaches the maximum limit. Can any body help me??
相关问题
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Difference between Types.INTEGER and Types.NULL in
- What is the best way to cache a table from a (SQL)
- php PDO::FETCH_ASSOC doesnt detect select after ba
All that you need to simulate sequence objects in SQL Server.
Here is some code that works in SQL 2000:
Adapted from this: Dr. Tom's Workshop: Generating Sequence Numbers
As of SQL Server 2012, sequences have been added and are supposedly faster than IDENTITY.
Quick example using
CYCLE
option as the question requests but most people probably will not useCYCLE
because they'll use it for a synthetic primary key.Here's the SQL syntax taken from MSDN:
Here's how to do it in the GUI. This is the equivolent of the quick example above without the
CYCLE
option checked because most poeple won't use that:Notes:
There is no need for a while loop. First, you need a Tally or Numbers table:
I only put a 100K of numbers in my table but you might need more. You need only populate this table once.Now you can create any sequence you desire. Like so:
Want an increment value?: