Can a strored procedure contain Drop Sequence /Create Sequence statement?
I am trying to schedule a job to update the starting value of a sequence. So I need to write a strored procedure to drop and then recreate the sequence. But when i tried to cimpile the follwing stored procedue, it gave me the error "'DROP' is not a valid identifier". Is it illegal to have "Drop Sequence /Create Sequence " in a stored procedue? If this is true, is there any way to accomplish my task (simply recreate the same sequence with different starting value every month)? Thanks!
Begin
DROP SEQUENCE TEST_SEQ;
CREATE SEQUENCE TEST_SEQ
START WITH 0
MAXVALUE 999999999999
MINVALUE 0
NOCYCLE NOCACHE NOORDER;
End
In general, it is a bad idea to try to encode information into a key because it
- complicates queries in the future
- complicates updates to the data in the future
- can easily be generated on the fly from other data elements
Inevitably, if you do something like what was proposed, you'll find out that the data encoded in the key gets out of date from the data stored in the row, that users are writing queries that do things like strip off the first 6 characters of the string in order to do date searches, and that users discover they want to be able to modify the data to fix the data that's encoded in the keys, which can be terribly expensive if there are child tables.
Rather than trying to do this, I would store a DATE value and a non-reset sequence. You can always generate the reference number for the user by doing a TO_CHAR on the DATE and concatenating the output of an appropriate ranking function. If the users decide that they need to correct the DATE of an event, the reference number will be updated automagically since it was never stored in the database. A number generated in the query can also be guaranteed to be gap-free, which tends to please users.
Justin
Similar Messages
-
Script to drop/re-create sequences is generated with errors
Hello,
I'm migrating an Oracle 10g instance running on Solaris 10 to an Oracle 11g instance running on RedHat Linux. I'm following the "Platform Migration" White Paper ( URLhttp://www.oracle.com/technology/deploy/availability/pdf/maa_wp_11g_platformmigrationtts.pdf ).
In the appendix, there's the code for a script that's supposed to generate an SQL script that drops and re-creates the sequences on the target machine:
set heading off feedback off trimspool on escape off
set long 1000 linesize 1000 pagesize 0
col SEQDDL format A300
spool tts_create_seq.sql
prompt /* ========================= */
prompt /* Drop and create sequences */
prompt /* ========================= */
select regexp_replace(
dbms_metadata.get_ddl('SEQUENCE',sequence_name,sequence_owner),
'^.*(CREATE SEQUENCE.*CYCLE).*$',
'DROP SEQUENCE "'||sequence_owner||'"."'||sequence_name
||'";'||chr(10)||'\1;') SEQDDL
from dba_sequences
where sequence_owner not in
(select name
from system.logstdby$skip_support
where action=0)
spool off
I have run it on my Solaris machine, and it generates a file that looks like this:
/* ========================= */
/* Drop and create sequences */
/* ========================= */
CREATE SEQUENCE "MDSYS"."TMP_COORD_OPS" MINVALUE 1000000 MAXVALUE 2000000 INCREMENT BY 1 START WITH 1000000 NOCACHE NOORDER CYCLE
CREATE SEQUENCE "MDSYS"."SDO_TOPO_TRANSACT_SUBSEQ" MINVALUE 1 MAXVALUE 999999999999999999999999999 INCREMENT BY 1 START WITH 1 NOCACHE ORDER NOCYCLE
I don't have any "drop sequence" commands, and there is no semicolon (;) at the end of the "create sequence" lines, so those are not executed.
I'm not at all familiar with PL/SQL, could somebody point me to the error?
Thank you,
AdrianWhy do you need the "REGEXP_REPLACE ()" function?
This would be simpler:
SET long 32000 longc 80 pages 0 lin 80 trims on
COL ddl wor
SELECT 'DROP SEQUENCE "'
|| sequence_owner
|| '"."'
|| sequence_name
|| '";'
|| CHR (10)
|| DBMS_METADATA.get_ddl ('SEQUENCE', sequence_name, sequence_owner)
|| ';'
|| CHR (10) DDL
FROM dba_sequences
WHERE sequence_owner NOT IN (SELECT NAME
FROM SYSTEM.logstdby$skip_support
WHERE action = 0);
/:p -
How to create stored procedure to drop and create table
Version: Oracle 10g
I am trying to create a stored procedure that will drop and create a table based on a select statement. I can create the table but I can't drop it.
CREATE OR REPLACE procedure EC_LOAD is
begin
INSERT INTO Sales_table
(FSCL_WK,
DIV,
ACCT_TYPE)
Select
FSCL_WK,
DIV,
ACCT_TYPE
from
sales_revenue;
end ecload;
I need to drop Sales_table before inserting the values. How do i do this?Or with a drop:
SQL> desc emp2
ERROR:
ORA-04043: object emp2 does not exist
SQL>
SQL> declare
2 cnt int := 0;
3 begin
4 select max (1)
5 into cnt
6 from user_tables
7 where table_name = 'EMP2';
8
9 if cnt = 1
10 then
11 execute immediate 'drop table emp2';
12 end if;
13
14 execute immediate 'create table emp2 as select * from emp';
15 end;
16 /
PL/SQL procedure successfully completed.
SQL>
SQL> desc emp2
Name Null? Type
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
SQL> -
How to CREATE SEQUENCE in one table
dear all
the one thing i want to know when i use CREATE SEQUENCE in one table like this and then at that table
CREATE SEQUENCE oproduct_sequence
START WITH 1 INCREMENT BY 1
nocache
create table oproduct(
tname varchar2(20) not null,
tid int default oproduct_sequence.nextval
the system indicat that i cannot use this way to create table , so i really want to know how can i achieve this method
becuase when i want to insert inot table oproduct only use
insert into oproduct valuse('aaaa');
and the result like
aaaaa 1
bbbb 2Actual name is before insert trigger. Some examples are listed below:
Example Number 1 ...
create sequence product_seq start with 1 increment 1
create or replace trigger product_insert before insert for each row begin
select productseq.nextval
into :new.product_id
from dual;
end;
Example Number 2 ...
How to create an autoincrement field in a table with a sequence ...
SQLWKS> create table bob(a number , b varchar2(21));
Statement processed.
First create a sequence
SQLWKS> create sequence x ;
Statement processed.
Then create the trigger.
create trigger y before insert on bob
for each row
when (new.a is null)
begin
select x.nextval into :new.a from dual;
end;
Example Number 3 ...
First create a sequence:
create sequence emp_no_seq;By default it increments by 1 starting at 0.
Use its values when inserting data into the table:
insert into t_emp values (emp_no_seq.nexval, 'Joe Black');~ Madrid. -
Error when creating Sequence inside a stored procedure
I tried to drop a sequence and then re-create it with new starting number in a pl/sql procedure by using 'execute immediate' command.
However, I got the 'ORA-01031: insufficient privileges' error when I actually executed the procedure.
I was able to create the sequence directly from SQL*Plus prompt. So, I believe I have the sufficient privilege to create sequence.
Why it's not working when it's in a procedure?
Please advise. Thanks!I'm not mean at all. You asked a redundant question, and you didn't research it or for less than 1 minute.
Doing so you add to the clutter, and contribute to turning this forum in a chatroom (which it isn't ) and to a place of garbage
(which it is thanks to you and thousands of other DBAs who never consult documentation before asking a question)
That's just the truth. I can't help it you can't bear to hear the truth. That is a problem with you, not with me.
I am in forums like this one for over 15 years. Over the years I have seen more and more people who don't want to learn to fish.
And as things are getting worser and worser: yes, it annoys me.
And I did anwer the question, didn't I. Frankly, all over the globe real DBAs are tearing their hair out (provided they still have it) because of the sort of 'applications' you and your colleagues develop.
They are a disaster.
Sybrand Bakker
Senior Oracle DBA -
CREATE SEQUENCE from a stored procedure
Hello,
Is it possible, to create a sequence object from an own written stored procedure? Can I reinitialize the actual value of a sequence object without recreating it from a stored procedure?
Thank you for recommendations,
Matthias Schoelzel
EDV Studio ALINA GmbH
Bad Oeynhausenmaybe this example might be of some help.
SQL> create or replace procedure dy_sequence (pSeqName varchar2,
2 pStart number,
3 pIncrement number) as
4 vCnt number := 0;
5 begin
6 select count(*) into vCnt
7 from all_sequences
8 where sequence_name = upper(pSeqName);
9
10 if vCnt = 0 then
11 execute immediate 'create sequence '||pSeqName||
12 ' start with '||to_char(pStart)||
13 ' increment by '||to_char(pIncrement);
14 else
15 execute immediate 'alter sequence '||pSeqName||' increment by '||to_char(pIncrement);
16 end if;
17 end;
18 /
Procedure created.
SQL> -- create the sequence by calling the dy_sequence procedure
SQL> execute dy_sequence ('test_sequence',1,1);
PL/SQL procedure successfully completed.
SQL> select test_sequence.nextval from dual;
NEXTVAL
1
SQL> -- alter the sequence to increment by 2
SQL> execute dy_sequence ('test_sequence',0,2);
PL/SQL procedure successfully completed.
SQL> select test_sequence.nextval from dual;
NEXTVAL
3
SQL> -
Problem with creating sequence in procedure
I am getting the following error when trying to create a sequence inside a procedure:
ORA-01031: insufficient privileges
But, I have no problem creating the sequence outside the procedure, by just issuing the create sequence command.
How can I make this work?
Thanks.I am getting different result for this:
INSERT INTO DEPNODE.SEQ_MEID (proc_id, me_id, seq_id)
SELECT 'tstamp' AS proc_id, M.MANAGED_ENTITY_ID AS ME_ID, ROW_NUMBER () OVER (ORDER BY M.MANAGED_ENTITY_ID) AS seq_id
FROM FDM.MANAGED_ENTITIES M, FDM.PHYSICAL_ADDRESSES P
WHERE M.MANAGED_ENTITY_ID = P.ME_MANAGED_ENTITY_ID
AND M.MANAGED_ENTITY_ID < 5;
If I ran this at the prompt, I get the correct result:
tstamp 1 1
tstamp 2 2
tstamp 3 3
tstamp 4 4
But, if its part of the procedure, I get this:
tstamp 1 1
tstamp 1 2
tstamp 1 3
tstamp 1 4
** the middle row is the seq_id
Thanks. -
How can i pass the parameter for strored procedure from java
dear all,
I am very new for stored procedure
1. I want to write the strored procedure for insert.
2. How can i pass the parameter for that procedure from java.
if any material available in internet create procedure and call procedure from java , and passing parameter to procedure from javaHi Ram,
To call the callable statement use the below sample.
stmt = conn.prepareCall("{call <procedure name>(?,?)}");
stmt.setString(1,value);//Input parameter
stmt.registerOutParameter(2,Types.BIGINT);//Output parameter
stmt.execute();
seq = (int)stmt.getLong(2);//Getting the result from the procedure. -
How can I return the nextval of a particular sequence from a stored procedure?
I have a stored procedure that basically generates and records
nextval into a table each time the procedure is called. I want
to return this value in an out parameter. Is this possible? If
so, how do I implement it?sq> create sequence s1 start with 1 increment by 1;
procedure to get the nextval from a sequence:
sql>create or replace procedure p1
(seq_no_p OUT Number)
is
Begin
select s1.nextval into seq_no_p from dual;
End p1;
to call the above procedure to get the out parameter(nextval):
declare
p number;
begin
p1(p);
end;
If this is what u want.
Thiru -
Can drag and drop interactions created in Captivate 6.1 be exported to HTML5?
I need to convert some Flash drag and drop interactions to HTML5 and I'm looking at Captivate and Storyline.
Can drag and drop interactions created in Captivate 6.1 be exported to HTML5?
(I tried Captivate 6 with the drag and drop widget and that didn't work, and I can't find a trial version of 6.1).
Many thanks,
SonyaMany thanks Lilybiri
I've just chatted to Adobe support and they have told me there is no educational discount for subscription I coudn't find the info on the website, so if anyone is interested, subscription is $29.99/month or $19.99/month if you sign up for a year.
Cheers,
Sonya -
Can i create sequences padded with 0's in the beginning, for e.g. 00500
Hi,
If i want to create a sequence starting from 00500 to 99999, how can i do that?
Please note the sequence should start with 00500 and not 500. I tried creating one, but 00500 got converted to 500.SQL> with t as
2 (select 100 c1 from dual union all
3 select 1000 from dual)
4 select lpad(to_char(c1),5,'0')
5 from t;
LPAD(
00100
01000 -
How can I insert a spreadsheet containing drop down boxes and links to other spreadsheets into a keynotes slide? Please help
I need to retain all the formula's and the sheets these formulas are linked from.Have you considered switching out of Keynote and over to your spreadsheet program when you get to this slide? If you launch the speadsheet program before Keynote you can switch using Command-Tab. You can then switch back into Keynote when you are done.
I realize that this is not what you were looking for but I don't think it is possible to do exactly what you want. -
Sequence created in anon PL/SQL missing initial value
If I create a sequence in an anonymous PL/SQL block, with execute immediate, then perform a select nextval from that sequence outside the block, it misses it's initial value.
Easily reproducable here:
create table tab_a ( col_a number(4) not null );
begin
execute immediate 'create sequence seq_a start with 47 increment by 1';
end;
insert into tab_a select seq_a.nextval from dual;
select * from tab_a;
This is a much simplified representation of the circumstance I found myself in, but it reproduces the effect I see.
If I drop the sequence and truncate the table, and run the statements (excluding the first) again, it works as expected. If I drop the table and run all the statements again, it returns the wrong answer again.
I don't need a workaround, the issue has been addressed long ago, but I would like to know WHY this happens. starting the sequence at 47 can be any number, it's there just to show the issue when 48 is inserted into the table. The insert could be into any table, and the from could be over any table rather than dual.
Oracle EE 11.2.0.1.0 on OE Linux, RAC and Single node.
TIA.Can you show the output you get like this? Mine is a 10g DB. But sure it would work in 11g DB as well.
SQL> select * from v$version where rownum = 1;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi
SQL> drop table t;
Table dropped.
SQL> create table t (no integer not null);
Table created.
SQL> begin
2 execute immediate 'create sequence seq start with 47 increment by 1';
3 end;
4 /
PL/SQL procedure successfully completed.
SQL> insert into t select seq.nextval from dual;
1 row created.
SQL> select * from t;
NO
47
SQL> -
Hi All,
I am using
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bi
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - ProductionI have schema with following roles
ALTER ANY OUTLINE
CREATE ANY OUTLINE
CREATE ANY SYNONYM
CREATE DATABASE LINK
CREATE MATERIALIZED VIEW
CREATE PUBLIC DATABASE LINK
CREATE VIEW
DROP ANY OUTLINE
DROP PUBLIC DATABASE LINK
SELECT ANY TABLE
UNLIMITED TABLESPACE
CONNECT
DBA
EXP_FULL_DATABASE
IMP_FULL_DATABASE
OEM_MONITOR
RESOURCEI can able create a sequence through SQLPLUS as well as from toad.
SQL> CREATE SEQUENCE ROLE_ACTIVITY_SEQ START WITH 225006249 INCREMENT BY 1 MAXVALUE 9999999999999999999 MINVALUE 225006249 NOCYCLE CACHE 100 ORDER;
Sequence created.
SQL>when i try to create sequence dynamically through PL/SQL procedure getting error
SQL> execute PROC_CLONE_BU;
BEGIN PROC_CLONE_BU; END;
ERROR at line 1:
ORA-01031: insufficient privileges
ORA-06512: at "SUPERNOVA.PROC_CLONE_BU", line 19
ORA-06512: at line 1
SQL>Even though I have DBA role for the schema. I don't know what privileges oracle still excepting..
Pls guide me to resolve it..
Thanks & Regards
SamiHi All,
As said early creating sequence through dynamic SQL
its creating sequence fine but when i try to use sequence in same procedure then procedure getting error..
create or replace procedure proc_seq_genrate as
begin
EXECUTE IMMEDIATE 'CREATE SEQUENCE BU_ROLE_DSK_ITEM_SEQ START WITH 100 INCREMENT BY 1 MAXVALUE 9999999999999999999 '||
'MINVALUE 100 NOCYCLE CACHE 100 ORDER';--- hard coded value 100 will changed according to PK max value of the table .. for getting max value i will write SQL Query
end;
Procedure created.
create or replace procedure proc_seq_genrate as
begin
EXECUTE IMMEDIATE 'CREATE SEQUENCE BU_ROLE_DSK_ITEM_SEQ START WITH 100 INCREMENT BY 1 MAXVALUE 9999999999999999999 '||
'MINVALUE 100 NOCYCLE CACHE 100 ORDER';--- hard coded value 100 will changed according to PK max value of the table .. for getting max value i will write SQL Query
INSERT INTO ROLE_ACTIVITY (SELECT ROLE_ACTIVITY_SEQ.NEXTVAL, -98, ACTIVITY_CD, REC_ST, 1, ROW_TS, USER_ID, CREATE_DT, SYS_CREATE_TS, CREATED_BY FROM ROLE_ACTIVITY WHERE ROLE_ID=-99 );
end;
7 35 PL/SQL: ORA-02289: sequence does not existI have understood that its looking for Sequence but
pls any one explain me why its looking sequence in compile time itself.. any way we are going to use the sequence only at run time right.. i would like to know is there any technical reason behind this..
Thanks & regards
Sami. -
Using dynamic query to create sequence
Hello,
I created the sequence dynamically in a Procedure, but when I executed, it gave me an Insufficient privileges error:
SQL> create table dummy (id number, module_id varchar2(20), p_order number, status varchar2(1));
SQL> insert into dummy values (10, 'test', 0, 'D');
SQL> CREATE OR REPLACE PROCEDURE PRO_SEQ_ARRNGE(P_ID NUMBER) AS
V_MOD DUMMY.MODULE_ID%TYPE;
v_query1 varchar2(200);
v_query2 varchar2(200);
V_COUNT NUMBER;
begin
v_query1 := 'drop sequence unqid';
v_query2 := 'create sequence unqid start with 1 increment by 1 minvalue 1';
SELECT COUNT(*)
INTO V_COUNT
FROM USER_SEQUENCES
WHERE SEQUENCE_NAME = 'UNQID';
IF V_COUNT = 0 THEN
execute immediate v_query2;
ELSE
execute immediate v_query1;
execute immediate v_query2;
END IF;
SELECT distinct MODULE_ID INTO V_MOD FROM DUMMY WHERE ID = P_ID;
update dummy
set P_order = 0, status = 'D'
WHERE ID = P_ID
and module_id = v_mod;
--COMMIT;
execute immediate 'UPDATE DUMMY SET P_ORDER = UNQID.NEXTVAL WHERE MODULE_ID = V_MOD AND STATUS = ''A''';
--COMMIT;
END PRO_SEQ_ARRNGE;
SQL> exec PRO_SEQ_ARRNGE(10);
BEGIN PRO_SEQ_ARRNGE(10); END;
ERROR at line 1:
ORA-01031: insufficient privileges
ORA-06512: at "SYSTEM.PRO_SEQ_ARRNGE", line 15
ORA-06512: at line 1
Can you please advise how to resolve it?
Thanks in advance,
TinkuWhen I try it, I get a different error
SQL> create table dummy (id number, module_id varchar2(20), p_order number, status varchar2(1));
Table created.
SQL> insert into dummy values (10, 'test', 0, 'D');
1 row created.
SQL> CREATE OR REPLACE PROCEDURE PRO_SEQ_ARRNGE(P_ID NUMBER) AS
2 V_MOD DUMMY.MODULE_ID%TYPE;
3 v_query1 varchar2(200);
4 v_query2 varchar2(200);
5 V_COUNT NUMBER;
6 begin
7 v_query1 := 'drop sequence unqid';
8 v_query2 := 'create sequence unqid start with 1 increment by 1 minvalue 1';
9 SELECT COUNT(*)
10 INTO V_COUNT
11 FROM USER_SEQUENCES
12 WHERE SEQUENCE_NAME = 'UNQID';
13
14 IF V_COUNT = 0 THEN
15 execute immediate v_query2;
16 ELSE
17 execute immediate v_query1;
18 execute immediate v_query2;
19 END IF;
20
21 SELECT distinct MODULE_ID INTO V_MOD FROM DUMMY WHERE ID = P_ID;
22
23 update dummy
24 set P_order = 0, status = 'D'
25 WHERE ID = P_ID
26 and module_id = v_mod;
27 --COMMIT;
28
29 execute immediate 'UPDATE DUMMY SET P_ORDER = UNQID.NEXTVAL WHERE MODULE_ID = V_MOD AND STATUS = ''A''';
30 --COMMIT;
31
32 END PRO_SEQ_ARRNGE;
33 /
Procedure created.
SQL> exec PRO_SEQ_ARRNGE(10);
BEGIN PRO_SEQ_ARRNGE(10); END;
ERROR at line 1:
ORA-00904: "V_MOD": invalid identifier
ORA-06512: at "SCOTT.PRO_SEQ_ARRNGE", line 29
ORA-06512: at line 1The problem is that you can't refer to a local variable like V_MOD in a dynamic SQL statement. You'd need to use a bind variable
SQL> ed
Wrote file afiedt.buf
1 CREATE OR REPLACE PROCEDURE PRO_SEQ_ARRNGE(P_ID NUMBER) AS
2 V_MOD DUMMY.MODULE_ID%TYPE;
3 v_query1 varchar2(200);
4 v_query2 varchar2(200);
5 V_COUNT NUMBER;
6 begin
7 v_query1 := 'drop sequence unqid';
8 v_query2 := 'create sequence unqid start with 1 increment by 1 minvalue 1';
9 SELECT COUNT(*)
10 INTO V_COUNT
11 FROM USER_SEQUENCES
12 WHERE SEQUENCE_NAME = 'UNQID';
13 IF V_COUNT = 0 THEN
14 execute immediate v_query2;
15 ELSE
16 execute immediate v_query1;
17 execute immediate v_query2;
18 END IF;
19 SELECT distinct MODULE_ID INTO V_MOD FROM DUMMY WHERE ID = P_ID;
20 update dummy
21 set P_order = 0, status = 'D'
22 WHERE ID = P_ID
23 and module_id = v_mod;
24 --COMMIT;
25 execute immediate 'UPDATE DUMMY SET P_ORDER = UNQID.NEXTVAL WHERE MODULE_ID = :1 AND STATUS = ''A'''
26 using v_mod;
27 --COMMIT;
28* END PRO_SEQ_ARRNGE;
29 /
Procedure created.
SQL> exec pro_seq_arrnge(10);
PL/SQL procedure successfully completed.Of course, I'm not using the SYSTEM schema. You should really, really avoid SYS and SYSTEM-- things often work differently there than they do normally. I also join the other folks that have tried to help you in suggesting that creating a sequence dynamically in a procedure is a very poor idea and almost certainly indicates that you need to reconsider your design.
Justin
Maybe you are looking for
-
Is The Macbook Pro Widescreen?
It says it is 15.4", but nothing about widescreen. I've been told that intergers usually indicate a 4:3 ratio and the MBP definitely does not have the 4:3 ratio for pixels. Is it considered widescreened or just...slightly larger?
-
I am using the Migration Assistant for MS Access (version 8.1.5) to migrate a database from Access 97 to Oracle 8i. At the Provide Oracle Connection Information Page of the Assistant, I get a Specifying Net8 Connection Error. So my question is what s
-
Schema owner identified externally
I have an oracle 11g, apex 3, OHS implement. The schema assigned to my workspace has owner, identified externally. I can find no workspace/schema relation administering Oracle-XE. Looks like the database username that logs in to the Administration Ho
-
Hello Experts, we are facing issue in Report painting for report GS38. basically there are two requirement 1. TOP WBS new field as selection field. 2. GS38 report is not fetching lower wbs cost in output of this report. So Can any Expert share his ex
-
Problem when create standby database
hello, I have two server(NT4.0,SP6) installed Oracle 8i(8.1.6 Enterprise) at the same directory.I want one as the primary database,another one as the standby database. The two database have the same SID,use SQL*Plus it can access each other. My opera