-- The followingCodeThis example shows how to generate a date number. The total length of the number is 8. The first five digits are the year and month information. The format is yymm, and the last four digits are the serial numbers.
-- Create a view for the current date
Create view v_getdate
As
Select dt = convert (char (4), getdate (), 12)
Go
-- Get the function with the new number
Create Function f_nextbh ()
Returns char (8)
As
Begin
Declare @ DT char (4)
Select @ dt = dt from v_getdate
Return (
Select @ DT + right (10001 + isnull (right (max (BH), 4), 0), 4)
From TB with (xlock, paglock)
Where BH like @ DT + '% ')
End
Go
-- Apply a function to a table
Create Table Tb (
BH char (8) primary key default DBO. f_nextbh (),
Col INT)
-- Insert data
Insert Tb (COL) values (1)
Insert Tb (COL) values (2)
Insert Tb (COL) values (3)
Delete TB where Col = 3
Insert Tb (COL) values (4)
Insert Tb (BH, col) values (DBO. f_nextbh (), 14)
-- Display Results
Select * from TB
/* -- Result
BH col
-------------------
05040001 1
05040002 2
05040003 4
05040004 14
(The number of affected rows is 4)
--*/
Go
Drop table TB
Drop function f_nextbh
Drop view v_getdate