在存储过程中自动生成ID [英] Auto generate id in stored procedure
本文介绍了在存储过程中自动生成ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要自动生成ID,例如abc00112(abc --some comp name,001-自动生成id,12-那年),我需要在存储过程中生成
@Quotationid bigint,
@说明varchar(max),
@Qty int,
@Duration int,
@成本钱,
@金额,
@总钱,
@日期datetime,
@Quotationnum varchar(50)
这些是我表中的列,我需要在"Quotationnum"中使用此ID.
i need to auto generate id like abc00112 (abc --some comp name,001 --auto generate id,12 --which year)like this i need to generate in my stored procedure
@Quotationid bigint,
@Description varchar(max),
@Qty int,
@Duration int,
@Cost money,
@Amount money,
@Total money,
@Date datetime,
@Quotationnum varchar(50)
these r the column in my table i need this id in "Quotationnum"
推荐答案
您可以使用IDENTITY和一个计算列来实现它:
You can achieve it usingan IDENTITY and a computed column as:
CREATE TABLE EMPLOYEE (
RealID int NOT NULL IDENTITY (1, 1),
Description varchar(max),
...
/*gives 000-999. Change the RIGHT as needed to give more*/
Quotationid AS 'abc' + RIGHT('000000000' + CAST(RealID as varchar(10)), 3)
CONSTRAINT PK_Quotation PRIMARY KEY CLUSTERED (Quotationid)
)
@GanesanSenthilvel ::: superb先生thanku thanku thanku这段代码对我有很大帮助.....:)
@GanesanSenthilvel:::superb sir thanku thanku thanku this code helps me a lot..... :)
这篇关于在存储过程中自动生成ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文