调用脚本的方式自动的创建或者是更新oracle数据库自带的Seq序列号的值
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。
* @author Alan
* @Email no008@foxmail.com
正文
执行脚本:
CREATE OR REPLACE PACKAGE PKG_QUERY IS -- Author : ADMINISTRATOR -- Created : 2016/12/8 星期四 10:28:37 -- Purpose : 用做查询游标 -- Public type declarations TYPE CUR_QUERY IS REF CURSOR; END PKG_QUERY; / create or replace function create_seq_func( seq_name in varchar2 ,/*seq名称*/ bus_table_name in varchar2,/*业务表名*/ pkcloumn_name in varchar2 /*业务表主键名*/ ) return varchar2 is result varchar2(30000); type ref_cursor_type is ref cursor; cursor_ids ref_cursor_type; cursor_ids_1 ref_cursor_type; V_NUM number:=0; v_max_seq number :=1; v_max_seq_1 number :=1; v_max_sql varchar(4000); v_max_sql_1 varchar(4000); v_sql_alter varchar(4000); v_sequence_sql varchar(4000); begin select count(0) into V_NUM from user_sequences where sequence_name = seq_name; if V_NUM > 0 then v_max_sql_1:='select to_number(max('||pkcloumn_name||')+100) from '||bus_table_name||' t '; dbms_output.put_line('_____0_______'||v_max_sql_1); open cursor_ids_1 for v_max_sql_1 ; fetch cursor_ids_1 into v_max_seq_1; /** 判断 是否有id 值查询出来 */ if cursor_ids_1%found then while cursor_ids_1%found loop dbms_output.put_line('_____1_______'||v_max_seq_1); ---把自动增长值设置为 业务表的最大值 v_sql_alter :='ALTER SEQUENCE '||seq_name||' INCREMENT BY '|| v_max_seq_1; dbms_output.put_line('_____3_______'||v_sql_alter); execute immediate v_sql_alter; ---获取下一个值 v_sql_alter :=' SELECT '||seq_name||'.NEXTVAL FROM DUAL '; dbms_output.put_line('_____4_______'||v_sql_alter); -- execute immediate v_sql_alter; open cursor_ids_1 for v_sql_alter ; fetch cursor_ids_1 into v_max_seq_1; /** 判断 是否有id 值查询出来 */ if cursor_ids_1%found then while cursor_ids_1%found loop dbms_output.put_line('_____4_1_______'||v_max_seq_1); v_max_seq:=v_max_seq_1; ---把自动增长值设置为1 v_sql_alter :='ALTER SEQUENCE '||seq_name||' INCREMENT BY 1 ' ; dbms_output.put_line('_____5_______'||v_sql_alter); execute immediate v_sql_alter; ---获取下一个值 v_max_sql_1 :=' SELECT '||seq_name||'.NEXTVAL FROM DUAL '; dbms_output.put_line('_____6_______'||v_max_sql_1); open cursor_ids_1 for v_max_sql_1 ; fetch cursor_ids_1 into v_max_seq_1; /** 判断 是否有id 值查询出来 */ if cursor_ids_1%found then while cursor_ids_1%found loop dbms_output.put_line('_____2_______'||v_max_seq_1); v_max_seq:=v_max_seq_1; fetch cursor_ids_1 into v_max_seq_1; exit when cursor_ids%notfound or cursor_ids%notfound is null ; end loop; end if; fetch cursor_ids_1 into v_max_seq_1; exit when cursor_ids%notfound or cursor_ids%notfound is null ; end loop; end if; fetch cursor_ids_1 into v_max_seq_1; dbms_output.put_line('_____2_______'||v_max_seq_1); exit when cursor_ids%notfound or cursor_ids%notfound is null ; end loop; end if; else v_sequence_sql :='create sequence '||seq_name||' minvalue 1 maxvalue 999999 start with 1 increment by 1 cache 20'; dbms_output.put_line('_____11_______'||v_sequence_sql); execute immediate v_sequence_sql; v_max_sql_1:='select to_number(max('||pkcloumn_name||')+100) from '||bus_table_name||' t '; dbms_output.put_line('_____10_______'||v_max_sql_1); open cursor_ids_1 for v_max_sql_1 ; fetch cursor_ids_1 into v_max_seq_1; /** 判断 是否有id 值查询出来 */ if cursor_ids_1%found then while cursor_ids_1%found loop dbms_output.put_line('_____11_______'||v_max_seq_1); ---把自动增长值设置为 业务表的最大值 v_sql_alter :='ALTER SEQUENCE '||seq_name||' INCREMENT BY '|| v_max_seq_1; dbms_output.put_line('_____13_______'||v_sql_alter); execute immediate v_sql_alter; ---获取下一个值 v_sql_alter :=' SELECT '||seq_name||'.NEXTVAL FROM DUAL '; dbms_output.put_line('_____4_______'||v_sql_alter); -- execute immediate v_sql_alter; open cursor_ids_1 for v_sql_alter ; fetch cursor_ids_1 into v_max_seq_1; /** 判断 是否有id 值查询出来 */ if cursor_ids_1%found then while cursor_ids_1%found loop dbms_output.put_line('_____4_1_______'||v_max_seq_1); v_max_seq:=v_max_seq_1; ---把自动增长值设置为1 v_sql_alter :='ALTER SEQUENCE '||seq_name||' INCREMENT BY 1 ' ; dbms_output.put_line('_____5_______'||v_sql_alter); execute immediate v_sql_alter; ---获取下一个值 v_max_sql_1 :=' SELECT '||seq_name||'.NEXTVAL FROM DUAL '; dbms_output.put_line('_____6_______'||v_max_sql_1); open cursor_ids_1 for v_max_sql_1 ; fetch cursor_ids_1 into v_max_seq_1; /** 判断 是否有id 值查询出来 */ if cursor_ids_1%found then while cursor_ids_1%found loop dbms_output.put_line('_____2_______'||v_max_seq_1); v_max_seq:=v_max_seq_1; fetch cursor_ids_1 into v_max_seq_1; exit when cursor_ids%notfound or cursor_ids%notfound is null ; end loop; end if; fetch cursor_ids_1 into v_max_seq_1; exit when cursor_ids%notfound or cursor_ids%notfound is null ; end loop; end if; fetch cursor_ids_1 into v_max_seq_1; dbms_output.put_line('_____17_______'||v_max_seq_1); exit when cursor_ids%notfound or cursor_ids%notfound is null ; end loop; end if; end if; return v_max_seq; EXCEPTION WHEN OTHERS THEN RETURN v_max_seq; end; /
脚本调用
使用plsql 的命令窗口调用:
为人:谦逊、激情、博学、审问、慎思、明辨、 笃行
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
.....................................................................
------- 桃之夭夭,灼灼其华。之子于归,宜其室家。 ---------------
------- 桃之夭夭,有蕡其实。之子于归,宜其家室。 ---------------
------- 桃之夭夭,其叶蓁蓁。之子于归,宜其家人。 ---------------
=====================================================================
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。 * @author Alan -liu * @Email no008@foxmail.com
转载请标注出处! ✧*꧁一品堂.技术学习笔记꧂*✧. ---> https://www.cnblogs.com/ios9/
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
.....................................................................
------- 桃之夭夭,灼灼其华。之子于归,宜其室家。 ---------------
------- 桃之夭夭,有蕡其实。之子于归,宜其家室。 ---------------
------- 桃之夭夭,其叶蓁蓁。之子于归,宜其家人。 ---------------
=====================================================================
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。 * @author Alan -liu * @Email no008@foxmail.com
转载请标注出处! ✧*꧁一品堂.技术学习笔记꧂*✧. ---> https://www.cnblogs.com/ios9/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2017-10-10 JProfiler 入门教程
2017-10-10 Web网站的性能测试工具
2017-10-10 职场中必须知道的8条规则
2017-10-10 作为测试人员需要的技能和工作要求
2017-10-10 web性能监控与分析
2017-10-10 Java应用常用性能分析工具
2017-10-10 mockito简单教程