I want to create a sequence in oracle where the max value of the column field (Empid) must be the min value of the sequence.
The below was the one i found in our same stackexchange
create sequence mytemp_seq start with &v_Startval;
This command prompts me to enter the max value of teh column name which i have to enter.
How can I fix the value of &v_startval with out it prompting ,but directly setting the values from the below statement
select max(empid) from mytemp..
I am trying like this below
create sequence mytemp_seq start with (SELECT MAX(empid) from mytemp)
But it doesnt work.
You could do it with some PL/SQL:
declare
v_startval integer;
begin
select max(empid)+1 into v_startval from mytemp;
execute immediate 'create sequence mytemp_seq start with ' || v_startval;
end;
In sqlplus you can do
col max_id new_value seq_min_val
SELECT MAX(empid)+1 AS max_id from mytemp;
create sequence mytemp_seq start with &seq_min_val;
Related
I seem to get ORA-00984 error when inserting the inputs to the sql statment
PLSQL code:
CREATE OR REPLACE PROCEDURE do_insättning (pnr_in in insättning.pnr%type,
knr_in in insättning.knr%type,
belopp_in in insättning.belopp%type,
datum_in in insättning.datum%type)
IS
BEGIN
INSERT INTO insättning
VALUES (radnr_seq,pnr_in,knr_in,belopp_in,datum_in);
END;
The design of the table:
Also the radnr_seq:
CREATE SEQUENCE radnr_seq START WITH 1 INCREMENT BY 1;
What I have tried is to check is that the sql statemtn only inserts to one column like this:
INSERT INTO insättning (radnr) values (radnr_seq);
Aswell as checking for invalid values but to no result.
You need to insert the sequence number nextval radnr_seq.nextval.
You need to call nextval for sequence :
CREATE OR REPLACE PROCEDURE do_insättning (pnr_in in insättning.pnr%type,
knr_in in insättning.knr%type,
belopp_in in insättning.belopp%type,
datum_in in insättning.datum%type)
IS
BEGIN
INSERT INTO insättning
VALUES (radnr_seq.NEXTVAL,pnr_in,knr_in,belopp_in,datum_in);
END;
my Oracle SqlDeveloper (or Oracle Database?) doesn't know the :NEW keyword.
For instance, if I enter the following sample from Oracles website,
when I execute the "create or replace trigger" paragraph, a window "Enter bind variable" pops up and asks for the bind variable ":new".
Shouldn't this ":new" variable be predefined?
(Oracle SQL Developer 4.0.1.14, Oracle DB 11gR2, Windows)
drop table tab1;
create table tab1 (c1 clob);
insert into tab1 values ('testtext');
create or replace trigger trg1
before update on tab1
for each row
begin
dbms_output.put_line('Old value of CLOB column: '||:OLD.c1);
dbms_output.put_line('Proposed new value of CLOB column: '||:NEW.c1);
-- Previously, we couldn't change the new value for a LOB.
-- Now, we can replace it, or construct a new value using SUBSTR, INSTR...
-- operations for a CLOB, or DBMS_LOB calls for a BLOB.
:NEW.c1 := :NEW.c1 || to_clob('<hr><p>Standard footer paragraph.');
dbms_output.put_line('Final value of CLOB column: '||:NEW.c1);
end;
/
set serveroutput on;
update tab1 set c1 = '<h1>Different Document Fragment</h1><p>Different text.';
select * from tab1;
It turned out I just had to press "Apply" in the Bind dialog, and the trigger was created. Obviously a bug in SqlDeveloper. At least there is a workaround...
I added the 'REFERENCING' statement, and tested the code, seems to work just fine--- BTW, you can just turn DBMS output on in SQL dev w/o having to run the command..
CREATE OR REPLACE TRIGGER trg1
BEFORE UPDATE
ON tab1
REFERENCING NEW AS new OLD AS old
FOR EACH ROW
BEGIN
DBMS_OUTPUT.put_line ('Old value of CLOB column: ' || :old.c1);
DBMS_OUTPUT.put_line ('Proposed new value of CLOB column: ' || :new.c1);
-- Previously, we couldn't change the new value for a LOB.
-- Now, we can replace it, or construct a new value using SUBSTR, INSTR...
-- operations for a CLOB, or DBMS_LOB calls for a BLOB.
:new.c1 := :new.c1 || TO_CLOB ('<hr><p>Standard footer paragraph.');
DBMS_OUTPUT.put_line ('Final value of CLOB column: ' || :new.c1);
END;
My Goal: I want to create a procedure where the min value of the
sequence will start at the max value primary key column in a table. So
that I can use this sequence to populate my primary key columns in ETL
maps without any constraint errors. Below is the code I'm trying to
work on, but I keep getting too many errors.
CREATE OR REPLACE PROCEDURE SET_MDM_IPEDS_SEQ
(P_NAME IN VARCHAR2,P_VAL IN OUT NUMBER)
IS
V_NUM NUMBER;
BEGIN
SELECT COALESCE(MAX(IPEDS_ORG_LND_KEY),0) INTO P_VAL FROM MDM_IPEDS_ORG_LND;
SELECT MDM_IPEDS_SEQ.NEXTVAL FROM DUAL INTO V_NUM;
ALTER SEQUENCE MDM_IPEDS_SEQ INCREMENT BY V_VAR;
SELECT MDM_IPEDS_SEQ.NEXTVAL FROM DUAL INTO V_NUM;
ALTER SEQUENCE MDM_IPEDS_SEQ INCREMENT BY 1;
END;
\
Try putting the ALTER SEQUENCE commands inside of an 'execute immediate'.
I want to add a column in a table every time the loop is run. I created a temporary variable which would take a different name for each iteration in the loop. now I want to add a column in my table with same name. I tried something like this, part of code is shown in oracle sql developer:
DECLARE V_COUNT VARCHAR (10) := ZIP_COD
BEGIN
ALTER TABLE ABCD
ADD V_COUNT varchar(10);
update ABCD b
set b.V_COUNT = (select d.ZIP_cod
from ORA_DMSTORE d
where b.id_dmstore = d.id_dmstore);
END;
this is showing the error " Usually a PL/SQL compilation error."
i don't know how to get rid of it. please help
First, you can't do ALTER TABLE .. in PL/SQL, you have to use dbms_sql or execute immediate:
And column names can't start with numbers, so you need some kind of prefix, n in my example.
So one possible solution would be:
DECLARE
V_COUNT VARCHAR (10) := ZIP_COD;
BEGIN
EXECUTE IMMEDIATE 'ALTER TABLE ABCD ADD (n'||V_COUNT||' varchar(10))';
EXECUTE IMMEDIATE '
update ABCD b
set b.n'||V_COUNT||' = (select d.ZIP_cod
from ORA_DMSTORE d
where b.id_dmstore = d.id_dmstore)
';
END;
/
SET SERVEROUTPUT ON
VARIABLE dept_id NUMBER
DECLARE
max_deptno NUMBER(3);
dept_name departments.department_name%TYPE :='Education';
BEGIN
SELECT MAX(department_id)
INTO max_deptno
FROM departments;
DBMS_OUTPUT.PUT_LINE ('The maximum department no is : ' || max_deptno);
:dept_id:=(max_deptno+10);
INSERT INTO departments (department_name, department_id,location_id)
VALUES(dept_name, :dept_id, NULL);
DBMS_OUTPUT.PUT_LINE ('The number of rows affected : ' || SQL%ROWCOUNT);
END;
/
Error report:
ORA-01400: cannot insert NULL into ("SYSTEM"."DEPARTMENTS"."DEPARTMENT_ID")
ORA-06512: at line 10
01400. 00000 - "cannot insert NULL into (%s)"
*Cause:
*Action:
The maximum department no is : 190
I am getting this error while trying to execute the bind variable in oracle statment. But if i put some value instead of bind variable, i get this insert statement right. What am I doing wrong here?
I think the value of the bind variable is only set when the pl/sql block is finished. And it probably has to terminate normally.
One solution is to use max_deptno+10 in the insert insead of :dept_if. A better solution is to create another pl/sql variable and use that in the insert statement.
new_dept_id := max_deptno+10;
:dept_id := new_dept_id;
You also have to change the INSERT statement:
INSERT INTO departments (department_name,department_id,location_id)
VALUES(dept_name, new_dept_id, NULL);
I think this error is obtained because you use bind variable without using set autoprint on in start program.