oracle重置序列从指定数字开始的方法详解
重置oracle序列从指定数字开始 代码如下: declare n number(10); v_startnum number(10):=10000001;--从多少开始 v_step number(10):=1;--步进 tsql varchar2(200); v_seqname varchar2(200):='MIP_JF_SEQUENCE';--序列名 begin execute immediate 'select '
重置oracle序列从指定数字开始
代码如下:
declare
n number(10);
v_startnum number(10):=10000001;--从多少开始
v_step number(10):=1;--步进
tsql varchar2(200);
v_seqname varchar2(200):='MIP_JF_SEQUENCE';--序列名
begin
execute immediate 'select '||v_seqname||'.nextval from dual' into n;
n:=v_startnum-n-v_step;--从10000001开始
tsql:='alter sequence '||v_seqname||' increment by '|| n;
execute immediate tsql;
execute immediate 'select '||v_seqname||'.nextval from dual' into n;
tsql:='alter sequence '||v_seqname||' increment by '||v_step;
execute immediate tsql;
end;
不通过删除重建方式 重置序列值得简单方式。 一般来讲,序列在实际开发过程中是经常用到的一种对象,通过它来生成主键是非常方便的,但是有些时候我们需要将其重新置零,通常采用的方式就是删除后重新创建。
下面我们来看一下另外一种方式:
代码如下:
SQL> create sequence seq_1 increment by 1 start with 1 maxvalue 999999999;
序列已创建。
SQL> create or replace procedure seq_reset(v_seqname varchar2) as
2 n number(10);
3 tsql varchar2(100);
4 begin
5 execute immediate 'select '||v_seqname||'.nextval from dual' into n;
6 n:=-(n-1);
7 tsql:='alter sequence '||v_seqname||' increment by '|| n;
8 execute immediate tsql;
9 execute immediate 'select '||v_seqname||'.nextval from dual' into n;
10 tsql:='alter sequence '||v_seqname||' increment by 1';
11 execute immediate tsql;
12 end seq_reset;
13 /
过程已创建。
SQL> select seq_1.nextval from dual;
NEXTVAL
---------
2
SQL> /
NEXTVAL
---------
3
SQL> /
NEXTVAL
---------
4
SQL> /
NEXTVAL
---------
5
SQL> exec seq_reset('seq_1');
PL/SQL 过程已成功完成。
SQL> select seq_1.currval from dual;
CURRVAL
---------
1
SQL>
这样可以通过随时调用此过程,来达到序列重置的目的。
此存储过程写的比较仓促,还可以进一步完善,,在此就不再进一步讲述
Oracle重置序列(不删除重建方式)
Oracle中一般将自增sequence重置为初始1时,都是删除再重建,这种方式有很多弊端,依赖它的函数和存储过程将失效,需要重新编译。
不过还有种巧妙的方式,不用删除,利用步长参数,先查出sequence的nextval,记住,把递增改为负的这个值(反过来走),然后再改回来。
假设需要修改的序列名:seq_name
1、select seq_name.nextval from dual; //假设得到结果5656
2、alter sequence seq_name increment by -5655; //注意是-(n-1)
3、select seq_name.nextval from dual;//再查一遍,走一下,重置为1了
4、alter sequence seq_name increment by 1;//还原
可以写个存储过程,以下是完整的存储过程,然后调用传参即可:
代码如下:
create or replace procedure seq_reset(v_seqname varchar2) as n number(10);
tsql varchar2(100);
begin
execute immediate 'select '||v_seqname||'.nextval from dual' into n;
n:=-(n-1);
tsql:='alter sequence '||v_seqname||' increment by '|| n;
execute immediate tsql;
execute immediate 'select '||v_seqname||'.nextval from dual' into n;
tsql:='alter sequence '||v_seqname||' increment by 1';
execute immediate tsql;
end seq_reset;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The retention period of Oracle database logs depends on the log type and configuration, including: Redo logs: determined by the maximum size configured with the "LOG_ARCHIVE_DEST" parameter. Archived redo logs: Determined by the maximum size configured by the "DB_RECOVERY_FILE_DEST_SIZE" parameter. Online redo logs: not archived, lost when the database is restarted, and the retention period is consistent with the instance running time. Audit log: Configured by the "AUDIT_TRAIL" parameter, retained for 30 days by default.

The Oracle database startup sequence is: 1. Check the preconditions; 2. Start the listener; 3. Start the database instance; 4. Wait for the database to open; 5. Connect to the database; 6. Verify the database status; 7. Enable the service (if necessary ); 8. Test the connection.

Can't enable Wi-Fi calling on iPhone? Call quality is improved and you can communicate even from remote locations where cellular networks are not as strong. Wi-Fi Calling also improves standard call and video call quality. So, if you can't use Wi-Fi calling on your phone, these solutions might help you fix the problem. Fix 1 – Enable Wi-Fi Calling Manually You must enable the Wi-Fi Calling feature in your iPhone settings. Step 1 – For this, you have to open Settings. Step 2 – Next, just scroll down to find and open the “Phone” settings Step 3 – In the phone settings, scroll down and open the “Wi-Fi Calling” setting. Step 4 – In the Wi-Fi Calling page, change “This iPhone

To find the number of occurrences of a character in Oracle, perform the following steps: Get the total length of a string; Get the length of the substring in which a character occurs; Count the number of occurrences of a character by subtracting the substring length from the total length.

Oracle database server hardware configuration requirements: Processor: multi-core, with a main frequency of at least 2.5 GHz. For large databases, 32 cores or more are recommended. Memory: At least 8GB for small databases, 16-64GB for medium sizes, up to 512GB or more for large databases or heavy workloads. Storage: SSD or NVMe disks, RAID arrays for redundancy and performance. Network: High-speed network (10GbE or higher), dedicated network card, low-latency network. Others: Stable power supply, redundant components, compatible operating system and software, heat dissipation and cooling system.

The amount of memory required by Oracle depends on database size, activity level, and required performance level: for storing data buffers, index buffers, executing SQL statements, and managing the data dictionary cache. The exact amount is affected by database size, activity level, and required performance level. Best practices include setting the appropriate SGA size, sizing SGA components, using AMM, and monitoring memory usage.

To create a scheduled task in Oracle that executes once a day, you need to perform the following three steps: Create a job. Add a subjob to the job and set its schedule expression to "INTERVAL 1 DAY". Enable the job.

The amount of memory required for an Oracle database depends on the database size, workload type, and number of concurrent users. General recommendations: Small databases: 16-32 GB, Medium databases: 32-64 GB, Large databases: 64 GB or more. Other factors to consider include database version, memory optimization options, virtualization, and best practices (monitor memory usage, adjust allocations).
