当前位置:网站首页>Oracle reset sequence generator (non rebuild)
Oracle reset sequence generator (non rebuild)
2022-06-29 10:31:00 【zlbcdn】
1、 background
1.1 background
There are 5 A sequence generator ( Respectively :seq1、seq2、seq3、seq4、seq5), The function of each sequence is to produce [10000,19999)、[20000,29999)、[30000,39999)、[40000,49999)、[50000,59999) The numerical . Create a sequence of DDL as follows :
CREATE SEQUENCE seq1 START WITH 10001 MAXVALUE 19999 MINVALUE 10001 increment by 1 NOCYCLE NOCACHE ORDER;
The others are the same , The difference is just the starting value
1.2 problem
Now every night 00:00 There's a timed mission , The function is to delete the sequence generator , Then rebuild , Ensure that the daily sequence generator starts from the starting value .
There have been occasional problems recently , Inquiry , Reset the timing task of the sequence generator at run time , There are other scheduled tasks using these sequence generators , This causes the scheduled task to fail .
therefore , Site requirements : It is necessary to reset the sequence generator , And avoid the occurrence of exceptions .
therefore , Reset the sequence generator in a non reconstruction way
2、 Solution
establish procedure, as follows :
/* founder : Zheng Lin 2017-8-30 Method statement : Reset the sequence generator Parameter description :v_seqname Sequence generator name ;v_max_value The maximum value of the sequence Other instructions :cache Process by default */ create or replace procedure seq_reset(v_seqname varchar2,v_max_value number) as n number(10); tsql varchar2(100); begin --1、 Set to nocahe( Prevent emergence ora-04013 error ) execute immediate 'alter sequence '|| v_seqname ||' NOCACHE'; --2、 Get the current value of the sequence execute immediate 'select '|| v_seqname ||'.nextval from dual' into n; n :=v_max_value-n; --3、 Change step size tsql := 'alter sequence '|| v_seqname ||' increment by ' || n; execute immediate tsql; --4、 Get current value begin execute immediate 'select '|| v_seqname ||'.nextval from dual' into n; exception when others then if sqlcode='-8004' then tsql:='alter sequence '||v_seqname||' increment by 1'; execute immediate tsql; end if; end; --5、 Recovery step tsql := 'alter sequence ' || v_seqname ||' increment by 1'; execute immediate tsql; --6、 recovery cache execute immediate 'alter sequence '|| v_seqname ||' CACHE 20'; end seq_reset;
Parameters in :v_seqname Is the name of the sequence generator ;v_max_value Is the maximum value of the sequence generator
such , Rewrite scheduled tasks , Can solve the current problem
SQL> set serveroutput on; SQL> exec seq_reset('SRXS001',19999); PL/SQL procedure successfully completed
3、 Reference material
1、 dynamic SQL, Reference resources : dynamic SQL The data link
2、 Non reconstruction methods , Reference resources : Non reconstituted Links
4、C# call procedure
// Get the set data
private int executeProcedure()
{
int result = 1;
// Get database connection
OracleConnection conn = DataBaseContext.GetSequenceOracleConnection();
OracleCommand command = new OracleCommand();
try
{
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
List<SeqModel> seqList = getSeqList();
// Complete execution procedure Methods
seqList.ForEach(t =>
{
result = result * executeProcedure(t.SeqName, t.MaxValue, command);
});
conn.Close();
return result;
}
catch (System.Exception ex)
{
SysLog.LogWrite("pharmacy.log", ex);
return -1;
}
finally
{
if (conn != null) conn.Close();
}
}
// perform procedure Methods
private int executeProcedure(string sequence_name, int max_value,OracleCommand command)
{
int execute_result = 0;
try
{
command.CommandText = "seq_reset";
command.Parameters.Clear();
command.Parameters.Add("v_seqname", OracleDbType.Varchar2).Direction = ParameterDirection.Input;
command.Parameters["v_seqname"].Value = sequence_name;
command.Parameters.Add("v_max_value", OracleDbType.Int32).Direction = ParameterDirection.Input;
command.Parameters["v_max_value"].Value = max_value;
execute_result = command.ExecuteNonQuery();
return execute_result;
}
catch (System.Exception ex)
{
SysLog.LogWrite("pharmacy.log", ex);
return -1;
}
}
边栏推荐
- Beautiful ruins around Kiev -- a safe guide to Chernobyl!
- [51nod 1215] array width
- September 25, 2020 noncopyable of boost library for singleton mode
- C language library function --strstr()
- L2-3 这是二叉搜索树吗?-题解超精彩哦
- 两个栈的模拟题
- Learn spark computing framework in practice (00)
- 2021 team programming ladder competition - Simulation Competition
- MySQL innodb每行数据长度的限制
- September 21, 2020 referer string segmentation boost gateway code organization level
猜你喜欢

Download control 1 of custom control (downloadview1)

Win32Exception (0x80004005): 组策略阻止了这个程序。要获取详细信息,请与系统管理员联系。

IIS server related error

Recurrence of vulnerability analysis for Cisco ASA, FTD and hyperflex HX

产品力不输比亚迪,吉利帝豪L雷神Hi·X首月交付1万台

這個開源項目超哇塞,手寫照片在線生成

Voir le classement des blogs pour csdn

Recyclerview sticky (suspended) head

HDU 6778 car (group enumeration -- > shape pressure DP)

1146 Topological Order (25 分)
随机推荐
2020-10-17:刷题1
Oracle重置序列发生器(非重建)
To 3 --- 最后的编程挑战
2019.11.13 training summary
Function pointer, function pointer array, calculator + transfer table, etc
520 diamond Championship 2021
View CSDN blog rankings
拼图小游戏中学到的Graphics.h
2019.10.16 training summary
Ce projet Open source est super wow, des photos manuscrites sont générées en ligne
Talk about threads and concurrency
L2-025 divide and rule (25 points)
Substring score - Ultra detailed version - the last programming challenge
Wandering --- 最后的编程挑战
Web漏洞手动检测分析
MySQL innodb每行数据长度的限制
在实践中学习Spark计算框架(00)
The process of updating a record in MySQL
Download control 1 of custom control (downloadview1)
Wandering -- the last programming challenge