请帮我写一个自动生成的编号,如存储过程...如何编写生成唯一标识符的存储过程?
ABC0000001
应该发生,因为我们让每个条目通过asp.net页面。我的意思是每次页面加载时都应该创建这个唯一的编号。
请帮我写一个自动生成的编号,如存储过程...如何编写生成唯一标识符的存储过程?
ABC0000001
应该发生,因为我们让每个条目通过asp.net页面。我的意思是每次页面加载时都应该创建这个唯一的编号。
我假设你想把这个数字存储在数据库的某个地方?所以也许这是一个插入存储过程,添加一个新的页面?
您将不得不在某处存储数字,以便存储过程知道下一个数字应该是什么。所以......为什么不把它作为一个整数存储,用uniqueidentifier和identity插入 - 所以你添加的每一行都有下一个数字作为ID。
如果你真的需要它在前面有'ABC',那么当你返回值时,只需将它添加到存储过程中的整数。
创建UDF PADL如下:
CREATE function PADL (@cSrting nvarchar(4000), @nLen smallint, @cPadCharacter nvarchar(4000) = ' ')
returns nvarchar(4000)
as
begin
declare @length smallint, @lengthPadCharacter smallint
select @length = datalength(@cSrting)/(case SQL_VARIANT_PROPERTY(@cSrting,'BaseType') when 'nvarchar' then 2 else 1 end) -- for unicode
select @lengthPadCharacter = datalength(@cPadCharacter)/(case SQL_VARIANT_PROPERTY(@cPadCharacter,'BaseType') when 'nvarchar' then 2 else 1 end) -- for unicode
if @length >= @nLen
set @cSrting = left(@cSrting, @nLen)
else
begin
declare @nLeftLen smallint, @nRightLen smallint
set @nLeftLen = @nLen - @length -- Quantity of characters, added at the left
set @cSrting = left(replicate(@cPadCharacter, ceiling(@nLeftLen/@lengthPadCharacter) + 2), @nLeftLen)+ @cSrting
end
return (@cSrting)
end
GO
您可以使用一个表来存储当前的数量和获得下一个数字,并使用PADL UDF垫与所需数量的“0”,然后CONCAT 'ABC'进入它。
下面的示例。您还可以添加请求日期时间的表,你也可以通过在你想存储有关的要求,如引用其他信息,方法等
create table RequestRecords (
ixRequestRecord int identity primary key,
sWhatever nvarchar(max) -- anything else you want to store
)
Create proc WebNewRequestEntry(@sWhatever nvarchar(max)
as
begin
insert RequestRecords(sWhatever) select @sWhatever
select 'ABC'+right('00000000000000' + cast(@@IDENTITY_INSERT as varchar(10)), 10) as sRequestRecord
end
通常我会创建一个“NumberSeries”表,其中存储号码系列的名称,最小号码,最大号码,前缀(在您的情况下为ABC
)和当前值。然后,对于并发的缘故,我创建了以下存储过程:
CREATE PROCEDURE [dbo].[spGetNextNo]
@series NVARCHAR(20)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @prefix NVARCHAR(10)
DECLARE @result NVARCHAR(50)
DECLARE @currvalue BIGINT
DECLARE @maxvalue BIGINT
DECLARE @errmsg NVARCHAR(250)
-- Find max value for number series
SELECT @prefix = Prefix, @maxvalue = EndValue FROM NumberSeries WHERE Series = @series
IF (@maxvalue IS NULL)
BEGIN
SET @errmsg = 'Number series "'[email protected]+'" does not exist!'
RAISERROR(@errmsg, 16, 1)
RETURN
END
-- Create next number
DECLARE @MyTableVar TABLE (CurrValue BIGINT)
UPDATE tNumberSeries
SET
CurrentValue = CurrentValue + 1
OUTPUT INSERTED.CurrentValue INTO @MyTableVar
WHERE
Series = @series
-- Number series used up?
SELECT TOP 1 @currvalue = CurrValue FROM @MyTableVar
IF (@currvalue >= @maxvalue)
BEGIN
SET @errmsg = 'Number series "'[email protected]+'" is used up!'
RAISERROR(@errmsg, 16, 1)
RETURN
END
SET @result = CAST(@currvalue AS NVARCHAR)
IF @prefix IS NOT NULL
SET @result = @prefix + @result
-- Return prefixed result
SELECT @result AS CurrentValue
RETURN 0
END
的UPDATE/OUTPUT
声明可确保电流值的方式,平行呼叫者在任何情况下获得唯一的数字增加。
我已经做过类似的事情对于需要沿着这些线路的项目数量或什么不同的业务对象:
declare @maxnum int
declare @myNumber @varchar(32)
select @maxnum = max(columnName) + 1 from theTable
select @myNumber = 'ABC' + replicate('0', 8 - len(@maxnum)) + cast(@maxnum as varchar)
您应该添加入住constraint包含该字段的表:
([ABC_Number] IS NOT NULL AND len([ABC_Number])=(10) AND substring([ABC_Number],(1),(3))='ABC' AND substring([ABC_Number],(4),(7)) like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9]')
通过这种方式,您可以确保从某处注入无效unique-identifier-format的记录是不可能的。
和独特的约束,以确保一些未在表中重复:
[dbo].[_CountAbcNumber]([abc_number])<(2)
其中_CountAbcNumber是这样的UDF:
CREATE FUNCTION [dbo].[_CountAbcNumber](
@AbcNumber AS nchar(10)
)
RETURNS int AS BEGIN
declare @retVal int
select @retVal = max(occurrences)
from (
select ABC_NUMBER, count(*) as occurrences
from dbo.YourTable
where ABC_NUMBER = @AbcNumber
group by ABC_NUMBER
) tmp
return @retVal;
END
请更新您的文章,包括你至今已写成。 – dandan78 2011-03-10 13:56:34
为什么它需要'ABC'在正面,正常的身份有什么问题?如果您需要额外的隔离代码,则将其分开存储并将其作为串联返回到视图中。 – Lazarus 2011-03-10 13:56:56
@Cheta,像ABC300001一样的数字是“XYZ0000001”...你至少需要描述你正在尝试生成的模式*。 **一个例子没有描述一个模式。** – Rob 2011-03-10 14:00:24