I am creating sequence in SQL Server with the following code. But it displays error as unknown object type. Please give a solution
Here's my code :
create sequence seqval start with 100 increment by 1 minvalue 0 maxvalue 0 no cycle
no cache;
thanks in advance
7条答案
按热度按时间vybvopom1#
You can do this.
qyzbxkaa2#
Create a
Numbers
table; here's a SO question on the subject. Let's call itdbo.Number
.Have a table with an identity column. Set the seed and step to whatever is appropriate:
Then insert values from the numbers table and capture the newly-generated identity values:
Be sure to
DELETE .. dbo.SequenceGenerator
from time to time, else it will get big for no additional value. Do notTRUNCATE
it - that will reset theIDENTITY
column to its initally-declared seed value.gdx19jrr3#
SQL Server 2008 can't create sequences, Sequence objects apply to SQL Server 2012 through current versions.
https://msdn.microsoft.com/es-es/library/ff878091(v=sql.120).aspx
You can use an IDENTITY in your table instead, for example:
The starting value for IDENTITY is 1, and it will increment by 1 for each new record.
http://www.w3schools.com/sql/sql_autoincrement.asp
ar7v8xwq4#
snvhrwxg5#
We can't use Sequence easily in SQL Server 2008.
You can use CTE(Common Table Expressions) for Sequence Generation in SQL Server 2008
1szpjjfi6#
Look at the following article: https://www.learnjavaupdate.com/2023/04/sequence-in-sql.html
The above script creates a sequence called
company_seq
that starts at 100 and increments by 1 for each subsequent call to theNEXTVAL
function. The sequence has a maximum value of 999, meaning that once the sequence reaches 999, it will stop generating values. TheNOCACHE
option specifies that Oracle should not cache sequence values, and theNOCYCLE
option specifies that the sequence should not cycle back to its starting value when it reaches its maximum value.Once the sequence is created, you can use the
NEXTVAL
function to generate unique values:klsxnrf17#
Are you sure you're running 2012? I had no trouble with:
Your 0,0 values generated a syntax error for me but a clear and simple one.