Hello Yan,
I was a bit wrong with the UDFs it is possible but for life I can't
get it tonight. Anyway below is a stored procedure that will do the
same as the Identity function in SQL. You can modify it to return any
range and format you want.
---------------------------------------------------------------------------
-- Identity Parameters Code
-- Version: 1.0
-- Date 1/08/2005
-- By Myles Matheson
--
http://bi-on-sql-server.blogspot.com/ --
-- This is script is without any warranty and like anything else
-- must be tested before use in a production system.
---------------------------------------------------------------------------
-- Create Table
CREATE TABLE [IdentityParameters] (
[TableName] [char] (10) NOT NULL ,
[TableColumnName] [char] (10) NOT NULL ,
[TableColumnLastValue] [char] (10) NOT NULL
) ON [PRIMARY]
GO
-- Insert Sample Values
-- TRUNCATE TABLE IdentityParameters
INSERT INTO [dbo].[IdentityParameters]
([TableName], [TableColumnName], [TableColumnLastValue])
VALUES('Product', 'ProductID', '10001')
GO
CREATE PROCEDURE spIdentityParameters
@TabName CHAR (10), @ColName CHAR (10)
AS
DECLARE
@CurrentID CHAR (10)
, @NextID CHAR (10)
BEGIN
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE -- NOT sure if this is
correct.
-- Need to maintain lock on IdentityParameters for the duration of the
transaction
-- Get the current id from IdentityParameters
SET @CurrentID = (SELECT TableColumnLastValue FROM IdentityParameters
WHERE @TabName = TableName
AND @ColName = TableColumnName)
-- SET Next ID to be incremented in any format.
-- Note this could be using a Aplha Numeric format.
-- This is just a counter for this example
SET @NextID = CAST (@CurrentID AS INT) + 100
-- Update Identity Parameters with the next id
--Should still be about of the same transaction
UPDATE IdentityParameters
SET TableColumnLastValue = @NextID
WHERE (@TabName = TableName
AND @ColName = TableColumnName)
SELECT @NextID AS ColID-- Return the new ID
END
go
-- Test code to return new ID
EXECUTE dbo.spIdentityParameters 'Product', 'ProductID'
-- Clean up
-- DROP TABLE IdentityParameters
-- DROP PROC spIdentityParameters
----------------------------------------------------------------------------------
Hope this helps
Myles Matheson
Data warehouse Architect
http://bi-on-sql-server.blogspot.com/