Oracle 脚本记录

给表创建序列或触发器 

create or replace procedure p_createseq(tablename in varchar2,key in varchar2)
Authid Current_User --使用角色权限,否则无法创建序列或表
is
strsql varchar2(500);
t_key  varchar2(500);
begin
--创建序列
strsql:='create sequence seq_'||tablename||
' minvalue 1 maxvalue 99999999 start with 1 increment by 1 nocache';
execute immediate strsql;
--设置触发器默认主键名
if(key is NULL) then t_key := 'ID' ;
else t_key := key;
end if;
--创建触发器
strsql := 'CREATE OR REPLACE TRIGGER TRG_'||tablename||'
BEFORE INSERT ON '||tablename ||'
FOR EACH ROW
BEGIN
SELECT SEQ_'||tablename||'.NEXTVAL INTO :NEW.'||t_key||' FROM DUAL;
END; ';
execute immediate strsql;
end p_createseq;

  Authid Current_User 指明使用当前角色权限,否则无法创建表或者序列。默认存储过程无法使用当前角色权限。

  调用存储过程,传递表名和主键名就好

创建表空间、用户、授权等

conn / as sysdba;
create tablespace jinwin;
create user jwst identified by jwst default tablespace jinwin temporary tablespace temp;
grant dba to jwst;
conn jwst/jwst;

 按字段分组,并取前N条

select * from ( select row_number() over (partition by sqbm  order by  SERIALNO_  ) RN from table) t where rn < 10

 

你可能感兴趣的:(oracle)