(1)通过存储过程方式生成(以Oracle为例)
Create Or Replace Procedure MyProcedure
{
CV_1 OUT NVARCHAR2
}
AS
BEGIN
DECLARE
V_CURRENTCODE NVARCHAR2(100)
BEGIN
SELECT MAX([编码字段名]) INTO V_CURRENTCODE FROM [表名];
IF substr(V_CURRENTCODE,8) = to_char(sysdate,'yyyymmdd') THEN
SELECT
CAST(
CAST(nvl(Max(substr([编码字段名] , 0 ,8)),0) AS nvarchar2(30)) || CAST(nvl(Max(substr([编码字段名] , 9 ,4)),0) AS nvarchar2(30))
AS INT)+1
AS [编码字段名]
INTO CV_1
FROM [表名]
dbms_output.put_line(CV_1);
ELSE
SELECT DISTINCT(to_char(sysdate,'yyyymmdd')|| '0001') as [编码字段名] into CV_1 from dual ;
dbms_output.put_line(CV_1);
END IF;
END;
至于调用存储过程我这里就不举例子了,微软有专门提供访问数据层中存在调用 ”存储过程“的方法。
(2)通过程序代码方式生成
/// <summary>
/// 生成流水号
/// </summary>
public class OrderHelper
{
/// <summary>
/// 帮助类
/// </summary>
private OrderHelper() { }
/// <summary>
/// 生成订单编号
/// 生成规则yyyyMMdd+四位数字
/// </summary>
/// <returns></returns>
public static string GetOrderCode(string code)
{
if (string.IsNullOrWhiteSpace(code)) return string.Format("{0}{1}", DateTime.Now.ToString("yyyyMMdd"), "0001");
if (code.TrimEnd().Length <= 11 || code.TrimEnd().Length >= 13) return "当前编码规则不正确!";
if (code.Substring(0, 8) == DateTime.Now.ToString("yyyyMMdd").TrimEnd())
{
if (Convert.ToInt32(code.Substring(9, 4)) >= 9999) return "当前编码规则已最大值,联系管理员扩大存储容量!";
return string.Format("{0}{1}", code.Substring(0, 8), (Convert.ToInt32(code.Substring(9, 4)) + 1).ToString());
}
else
return string.Format("{0}{1}", DateTime.Now.ToString("yyyyMMdd"), "0001");
}
}
只需调用静态方法即可返回编码。
本文章是新注册用户的第一篇文章,有不足之处,请留言赐教。。。