-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced project.sql
More file actions
77 lines (41 loc) · 1.98 KB
/
Copy pathadvanced project.sql
File metadata and controls
77 lines (41 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
--select * from user_cons_columns
--lumnsselect * from all_constraints
--select * from user_constraints ;
--select constraint_name
--from user_constraints u , user_tab_columns , user_cons_columns
--where u.constraint_type ='P' and user_tab_columns.data_type='NUMBER' and user_cons_columns.column_name = USER_TAB_COLUMNS.COLUMN_NAME
--select * from user_tab_columns;
--select * from user_cons_columns;
declare
cursor pk_con is
select distinct c.table_name,c.column_name as c_name
FROM user_constraints u , user_cons_columns c, user_tab_columns t
WHERE u.table_name = c.table_name AND U.CONSTRAINT_NAME = C.CONSTRAINT_NAME
and u.constraint_type ='P' and t.data_type ='NUMBER'
and C.COLUMN_NAME NOT IN ('START_DATE', 'JOB_ID', 'COUNTRY_ID');
v_max_id number(8,4);
cursor seq_cursor is select sequence_name from user_sequences;
begin
for seq_record in seq_cursor loop
execute immediate 'drop sequence '||seq_record.sequence_name;
end loop;
for table_record in pk_con loop
EXECUTE IMMEDIATE ' SELECT (NVL(MAX( '||table_record.c_name||' ),0)+1)
FROM HR.' || table_record.table_name
INTO v_max_id;
EXECUTE IMMEDIATE ' CREATE SEQUENCE '||table_record.table_name||'_SEQ
START WITH '||v_max_id||'
MAXVALUE 99999999999
MINVALUE 1
NOCYCLE
CACHE 20
NOORDER';
Execute immediate'CREATE OR REPLACE TRIGGER '||table_record.table_name||'_tr
BEFORE INSERT
ON '||table_record.table_name||'
FOR EACH ROW
BEGIN
:new.'||table_record.c_name||' := '||table_record.table_name||'_SEQ.nextval;
END;';
end loop;
end;