If you are familiar with ORACLE, you will find that no ready-made objects can be created.
Of course, you may also say that the most SQL method is to use the identity column, and the auto-increment method is OK. However, if you want to implement "YYYYMMDD" +, you can still use the stored procedure as you like.
A common online stored procedure is to create a table for All the data used in the current database. For example, "All" contains four fields: name, start value, increment value, and current value ], create a record to obtain
In systems with less concurrent requests, this process is fine. However, once a concurrent request is in a certain magnitude, this process will often encounter problems.
The following is an improvement method suitable for high-throughput access requests, such as thousands of requests per second:
-- Assume that you want to create a Sequence for T_0101001
-- Create a table SeqT_0101001
Create table SeqT_0101001 (
-- The ID column is an auto-increment column.
SeqID int identity (1, 1) primary key,
-- Sequence Value
SeqVal varchar (1)
)
-- Create a stored procedure for getting the latest Sequence value from the SeqT_0101001 table
Create procedure P_GetNewSeqVal_SeqT_0101001
As
Begin
-- Declare the new Sequence Value variable
Declare @ NewSeqValue int
-- Sets the number of items displayed after the insert or delete operation to cancel.
Set NOCOUNT ON
-- Insert a new value to the SeqT_0101001 table
Insert into SeqT_0101001 (SeqVal) values ('A ')
-- Set the new Sequence value to the last Identifier value inserted into the ID column of the SeqT_0101001 table.
Set @ NewSeqValue = scope_identity ()
-- Delete the SeqT_0101001 table (locked rows are not displayed)
Delete from SeqT_0101001 WITH (READPAST)
-- Returns the new Sequence value.
Return @ NewSeqValue
End
-- Use Sequence
Declare @ NewSeqVal int
Exec @ NewSeqVal = P_GetNewSeqVal_SeqT_0101001
Print @ NewSeqVal
To obtain the "20080724000056" format we mentioned just now, we can do the following:
Select Convert (char (8), Getdate (), 112) + right ('000000' + CAST (@ NewSeqVal AS varchar (5), 5) as mySeq
However, it is still useful. It cannot be directly used in select statements of non-stored procedures.
Reprinted from: http://www.cnblogs.com/heekui/archive/2008/07/24/1250842.html