create or replace procedure Test_GetOaUserInfo as
--authid current_user操作当前存储过程的当前用户,否则提示权限不足,但是这样存储过程这能执行一次
--,或者GRANT CREATE ANY TABLE TO any users;
timePoint Date;
tabCstr varchar2(1000); --创建表的字符串
varNum number(1) default 0; --定义统计记录变量默认为0
tabDstr varchar2(500); --存数据的字符串
tabData varchar2(500); -- 存数据的字符串
maxRow number;
begin
--抓取截止日期
select max(last_date) into timePoint from basedb_users;
--存取表的记录数
select count(*) into varNum from user_tables where table_name='BASEDB_USERS_TEMP_TB';
if timePoint is null then
begin
timePoint:=sysdate;
end;
end if;
dbms_output.put_line(to_char(timePoint,'YYYY-mm-dd hh24:mm:ss')); --set serveroutput on
dbms_output.put_line(varNum);
--创建临时表
if varNum < to_number(1) then --如果没有数据表创建临时表
--dbms_output.put_line('执行了啊');
tabCstr := 'create global temporary table BASEDB_USERS_TEMP_TB(
ID INTEGER not null,
LOGINID VARCHAR2(15),
FIRSTNAME VARCHAR2(30),
LASTNAME VARCHAR2(60),
FULLNAME VARCHAR2(100),
SEX CHAR(1),
JOBTITLE INTEGER,
SUBCOMPANYID INTEGER,
DEPARTMENTID INTEGER,
MANAGEID INTEGER,
WORKCODE VARCHAR2(15),
LAST_DATE DATE,
ACTION_SIGN CHAR(1),
STATUS CHAR(1),
PASSWORD VARCHAR2(50)
)ON COMMIT PRESERVE ROWS '; --ON COMMIT PRESERVE ROWS 会话级临时表 ,ON COMMIT delete ROWS 事务级临时表
execute immediate tabCstr;--执行DDL语句
end if;
--导入数据到临时表中
begin
tabData := 'insert into BASEDB_USERS_TEMP_TB(id,fullname)
select to_number(u.id) id,u.fullname from FMASTER.basedb_users@linkoa u where u.last_date
execute immediate tabData using timePoint;
commit;
end;
dbms_output.put_line(tabData);
select max(rownum) into maxRow from FMASTER.basedb_users@linkoa u where u.last_date < timePoint; --查询中最大记录数 commit; dbms_output.put_line('记录数'||maxRow); --删除临时表 --tabDstr := 'truncate table BASEDB_USERS_TEMP_TB '; --execute immediate tabDstr; --tabDstr := 'drop table BASEDB_USERS_TEMP_TB '; --execute immediate tabDstr; -- commit; end Test_GetOaUserInfo;
如果觉得《oracle删除定义变量 Oracle存储过程 临时表的创建 删除 变量的定义和使用》对你有帮助,请点赞、收藏,并留下你的观点哦!