最近用oracle写了个产生ID号得存储过程,有类似需求的可以参考
2011-08-31 09:35
204 查看
在ORACLE中产生流水号一般是序列的方式来生成。但是有时候也不那么方便,所以就写了一个存储过程来产生这个序列号。支持两种流水号,即00001,201108310001。
首先需要建立一个表:
create table T_ID
(
IDNAME VARCHAR2(40) not null,
IDCATE VARCHAR2(1),
IDLENGTH INTEGER,
IDVALUE VARCHAR2(40)
)
;
alter table T_ID
add constraint IDNAME_KEY primary key (IDNAME);
存储过程如下:
C#调用方式
首先需要建立一个表:
create table T_ID
(
IDNAME VARCHAR2(40) not null,
IDCATE VARCHAR2(1),
IDLENGTH INTEGER,
IDVALUE VARCHAR2(40)
)
;
alter table T_ID
add constraint IDNAME_KEY primary key (IDNAME);
存储过程如下:
create or replace procedure USP_GETID(mTable in varchar2, mCate in integer, mlength in integer,mFormat in varchar2,mValue out varchar2) as mZero varchar(40); tmpValue varchar(40); tmpCount number; begin mZero:= '000000000000000000000000000000'; mValue:= '1'; tmpValue:=''; select count(IDNAME) into tmpCount from t_id where IDNAME = mTable; if (tmpCount=1) then select idValue into tmpValue from t_id where IDNAME = mTable; end if; if (tmpValue is null or tmpValue='') then begin if (mCate=1) then mValue := to_char(substr(mZero,1,mlength -1)) || '' ||'1'; else mValue := to_char(sysdate,mFormat) || substr(mZero,1,mlength-1-length(mFormat)) || '1'; end if; insert into t_id values (mTable,to_char(mCate),mlength,mValue); end; else begin if (mCate=1) then begin tmpValue := to_number(tmpValue) + 1; mValue := to_char(substr(mZero,1,mlength - length(tmpValue))) || '' || to_char(tmpValue); end; else begin if (to_char(sysdate,mFormat)=substr(tmpValue,1,length(mFormat))) then begin tmpValue := to_char(to_number(substr(tmpValue,length(mFormat)+1,mlength-length(mFormat))) + 1); mValue := to_char(sysdate,mFormat) || substr(mZero,1,mlength -length(tmpValue)-length(mFormat)) || '' || to_char(tmpValue); end; else begin mValue := to_char(sysdate,mFormat)+ substr(mZero,1,mlength-length(mFormat)) ||'' || '1'; end; end if; end; end if; update t_id set IDVALUE=mValue,IDCATE=mCate,IDLENGTH=mlength where IDNAME=mTable; end; end if ; end USP_GETID;
C#调用方式
#region 通过存储过程产生编号 public string GetIDByProc(string Table, int len) { return GetIDByProc(Table, "1", len, ""); } public string GetIDByProc(string Table, int len, string DateFormat) { return GetIDByProc(Table, "0", len, DateFormat); } private string GetIDByProc(string Table, string idCate, int len, string DateFormat) { Database db = DatabaseFactory.CreateDatabase(); DbCommand command = db.GetStoredProcCommand("USP_GETID"); db.AddInParameter(command, "mTable", DbType.String, Table); db.AddInParameter(command, "mCate", DbType.Int32, idCate); db.AddInParameter(command, "mlength", DbType.Int32,len); db.AddInParameter(command, "mFormat", DbType.String,DateFormat); db.AddOutParameter(command, "mValue", DbType.String, 40); db.ExecuteNonQuery(command); object o = db.GetParameterValue(command, "mValue"); return Convert.ToString(o); } #endregion
相关文章推荐
- iOS 特定时间内才做某件事,有类似奇葩需求可以参考
- Kotlin通过Id操作View,Adapter和动态加载Xml文件也可以类似操作
- oracle权限相关(存储过程、JOB)存过必须要加上Authid Current_User is
- JAVA连接ORACLE的基本步骤,写在这里做备忘 (参考了http://student.csdn.net/space.php?uid=412909&do=blog&id=33102)
- 最近写的一个关于统计某个时间点所有组织内CAS 服务计数器的脚本,有需要可以将你的需求发给我!
- pl/sql游戏规则, 做Oracle开发的人员可以作为参考
- Oracle相同ID有多条记录,取时间最近的一条
- 产生自增长ID的存储过程
- oracle中怎么给一个非自增的平常字段加一个唯一id规则,可以是按年月日拼上一个3位数或者六位数
- oracle的SequenceGenerator为主键Id自动产生的序列号不是按顺序生成的解决办法
- oracle与sql server大批量存储过程可以替换部分
- 为oralce pl /sql脚本(包,函数,存储过程)产生类似java doc的文档
- 如何在Oracle 中实现类似自动增加 ID 的功能?
- oracle主键自动增长的实现 Oracle Sequence oracle maxid自动生成--ORA-00001: 违反唯一约束条件 (NAME.SYS_C005547)产生的原因之一(select * from all_cons_columns w
- oracle与sql server大批量存储过程可以替换部分
- 在多文档程序中,好像各种类都可以响应ON_COMMAND(ID_FILE_NEW, OnFileNew)类似的菜单响应。 那有没有什么样的原则?到底这些响应放到哪些类中比较合适? 比如有些菜单响应
- oracle实现类似mysql的LIMIT,也可以作为oracle分页使用
- 使用UUID可以产生一个号称全球唯一的ID
- 最近被一个问题困扰着,如何实现类似Photoshop的多层图像操作,每层可单独操作,又可以组合显示出来。
- MYSQL 存储过程实现类似ORACLE row_number 和 rank 的简单例子