ORA-06550 with alter statement [closed] - oracle

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying this code for some test and i got this error ORA-06550
I tried to modify column with set and many things still the same result
CODE :
declare
cursor cur is select comm from emp where comm is not null ;
enreg emp.comm%type;
moyene number;
somme number;
begin
open cur;
loop
fetch cur into enreg;
if emp.comm=enreg.com then
ALTER TABLE emp
set columun
emp.comm := enreg.com*100/emp.comm ;
end if ;
exit when cur%notfound;
end loop;
end;
the expected result is to change every emp.comm with the 10% of it but i got this error
Error :
ORA-06550: line 12, column 1:
PLS-00103: Encountered the symbol "ALTER" when expecting one of the following:
( begin case declare exit for goto if loop mod null pragma
raise return select update while with
<<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
1. declare
2. cursor cur is select comm from emp where comm is not null ;
3. enreg emp.comm%type;

In SQL, you use an UPDATE statement to change values in a table. ALTER TABLE is used to change the structure of a table, such as by adding columns, etc. You could rewrite your code as follows:
declare
cursor cur is select comm from emp where comm is not null;
enreg emp.comm%type;
moyene number;
somme number;
begin
open cur;
loop
fetch cur into enreg;
exit when cur%notfound;
if emp.comm = enreg then
update emp
set emp.comm = enreg * 100 / emp.comm;
end if ;
end loop;
commit;
end;
Best of luck.

Related

I am getting an error message in Oracle SQL stored procedure

I am trying the following code
CREATE OR REPLACE PROCEDURE sal2
AS
BEGIN
SELECT sal
FROM emp
END
And I'm getting this error
Error at line 7: PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
( begin case declare end exception exit for goto if loop mod
null pragma raise return select update while with
<< continue close current delete fetch lock
insert open rollback savepoint set sql execute commit forall
merge pipe purge
5. from emp
6. return 1
7. END
What I want to do is to get a sal column from emp table.
There are multiple issues with code as following;
create or replace procedure sal2
AS
Lv_var number; -- added variable for holding select value
BEGIN
select sal
Into lv_var -- into is needed in select
from emp; -- you missed this semicolon
-- you must use where clause as into variable can hold single value
-- so you must restrict this query to return only 1 record.
END; -- you missed this semicolon
/
Cheers!!
First you have to put the semicolon at the end of each line and after the END keyword. Then the SELECT statement is also wrong you have to load the result in a variable.
Here is the solution for multiple rows.
CREATE OR REPLACE PROCEDURE sal2
AS
CURSOR c_salaries IS SELECT salary FROM employees;
TYPE t_salaries IS TABLE OF c_salaries%rowtype INDEX BY BINARY_INTEGER;
v_salaries t_salaries;
BEGIN
OPEN c_salaries;
FETCH c_salaries BULK COLLECT INTO v_salaries;
CLOSE c_salaries;
END;
And one for a single row result.
CREATE OR REPLACE PROCEDURE sal2
AS
v_salary employees.salary%rowtype;
BEGIN
SELECT salary INTO v_salary
FROM employees WHERE employee_id = 100;
END;

PL/SQL error reference to uninitialised collection error even when its initialised

I have a PL/SQL script which used nested table. Below is the sample code.
type rec is record
(
--col data types here
)
type rec_table is table of rec;
v_rec_table rec_table := rec_table(); --initialising here.
arr_size integer := 0; --edit 1
...
...
begin
...
...
open cursor;
loop
fetch cursor bulk collect into v_rec_table limit arr_size; --edit
if nvl(v_rec_table.last,0) > 0 --it shows error is here.
then
...
...
end if;
The error is ORA-06531: Reference to uninitialized collection. Please help me debug this one.
If would help if you posted complete (sample) code (instead of "...").
Here's an example based on Scott's schema which works OK (your error line is line 14):
SQL> declare
2 type rec is record (id number);
3 type rec_table is table of rec;
4 v_rec_table rec_table := rec_table();
5
6 cursor c1 is select empno from emp;
7 begin
8 open c1;
9 loop
10 exit when c1%notfound;
11 fetch c1 bulk collect into v_rec_table;
12 end loop;
13 dbms_output.put_line('last = ' || v_rec_table.last);
14 if nvl(v_rec_table.last, 0) > 0 then
15 dbms_output.put_line('last exists');
16 end if;
17 close c1;
18 end;
19 /
last = 12
last exists
PL/SQL procedure successfully completed.
Though, I'm not sure what's that LOOP used for, as you could have done it as
SQL> declare
2 type rec is record (id number);
3 type rec_table is table of rec;
4 v_rec_table rec_table := rec_table();
5 begin
6 select empno bulk collect into v_rec_table from emp;
7 dbms_output.put_line('last = ' || v_rec_table.last);
8 end;
9 /
last = 12
PL/SQL procedure successfully completed.
Although your problem is not reproducible, there are few things I found could be improved in your code.
If you are using a bulk collect, the collection is initialised automatically and hence, this line is not required
v_rec_table rec_table := rec_table();
Also, as #Littlefoot mentioned, LOOP is not required unless you are using the LIMIT clause.
You may use the COUNT collection function instead of nvl(v_rec_table.last,0)
v_rec_table.count
Instead of defining a record type and bulk collecting into it, you may define a collection of cursor%ROWTYPE;
CURSOR cur is SELECT column1,column2 FROM tablename;
type rec_table is table of cur%ROWTYPE;
v_rec_table rec_table;
Apologies since i did not post whole code. The error occurred because i did not add index by to two columns in the record definition.
After reading through few articles i found my flaw.

Pass a table name to a function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm looking for a way to return one case-insensitive varchar2 from a table given as a parameter.
The database is configured to have as the first column the one I'll look into.
This is what I got to:
create or replace function generic_return(nam varchar2, table_n varchar2)
return varchar2
as
case_insensitive varchar2(300);
case_ins varchar2(300);
colu varchar2(30);
t_nam varchar2(30):=upper(table_n);
cursor point is execute inmediate select colu from t_nam;
cursor col is select column_name from cols where table_name=t_nam;
non_existent_table exception;
begin
case_ins:=upper(rtrim(ltrim(nam)));
open col;
fetch col into colu;
close col;
select column_name from cols where table_name=upper(table_n);
if colu is null then
raise non_existent_table;
end if;
open point;
loop
fetch point into case_insensitive;
exit when point%notfound;
if upper(case_insensitive)=case_ins then
return case_insensitive;
end if;
end loop;
close point;
return null;
end;
/
The function receives what to look for, and the name of the table to look into, then the first variables are to compare them, t_nam is to use the uppercased version of the second parameter... and from that there's the huge mess: I get the column name from the table in col, from cols, and then I try to make a cursor that goes around checking if what the tuple, going by the same modifications, is the first parameter, or not, if it happens to be, then it should return the unmodified version of the one in the table. Otherwise, it should return "null", which I'll treat differently on each procedure that uses it.
By null I mean nothingness, not the string.
Yes, you are right. That's a pretty huge mess. :-)
For starters, you cannot declare an explicit cursor as an execute immediate statement.
Next, if you want the first column in the table, you need to specify
WHERE column_id = 1
and then you can just grab it via a SELECT-INTO, no need for an explicit cursor.
Then you could try something like:
my_cur SYS_REFCURSOR;
BEGIN
...
OPEN my_cur FOR 'SELECT ' || first_col || ' FROM ' || t_name;
LOOP
FETCH my_cur INTO case_insensitive;
EXIT WHEN my_cur%NOTFOUND;
... logic for match ....
END LOOP;
CLOSE my_cur;

What is wrong with this PL/SQL [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have this pl/sql code block,
set serveroutput
clear screen;
DECLARE
TYPE recPriceList is Record(comno varchar2(3),t$cpls varchar2(6),t$dsca varchar(100), Customers int,minExpiry varchar2(20),groupCount int,undefGroups int);
comno VARCHAR2(6) :='010';
sSql VARCHAR2(1000):=' ';
c sys_refcursor;
r recPricelist;
BEGIN
sql:=q'[select distinct lpad('010',3,'0'), t$cpls cpls,t$dsca,count(t$cuno),null Customers,null,null
from baan.ttccom010010 c
join baan.ttcmcs034010 p on c.t$cpls = p.t$cplt
where trim(t$cpls) is not null
group by T$CPLS,T$DSCA order by t$cpls']';
OPEN c FOR sSql ;
LOOP
FETCH c INTO r;
DBMS_OUTPUT.PUT_LINE(r.comno||' | '||r.t$cpls|| ' dsca='|| r.t$dsca);
EXIT WHEN c%notfound;
END LOOP;
END;
I cant figure why the following error being thrown when I run this block
SQLPLUS command failed - not enough arguments Error starting at line : 5 in command - DECLARE TYPE recPriceList is Record(comno
varchar2(3),t$cpls varchar2(6),t$dsca varchar(100), Customers
int,minExpiry varchar2(20),groupCount int,undefGroups int); comno
VARCHAR2(6) :='010'; sSql VARCHAR2(1000):=' '; c
sys_refcursor; r recPricelist; BEGIN
sql:=q'[select distinct lpad('010',3,'0'), t$cpls
cpls,t$dsca,count(t$cuno),null Customers,null,null
from baan.ttccom010010 c
join baan.ttcmcs034010 p on c.t$cpls = p.t$cplt
where trim(t$cpls) is not null
group by T$CPLS,T$DSCA order by t$cpls']';
OPEN c FOR sSql ; LOOP
FETCH c INTO r;
DBMS_OUTPUT.PUT_LINE(r.comno||' | '||r.t$cpls|| ' dsca='|| r.t$dsca);
EXIT WHEN c%notfound; END LOOP; END; Error report - ORA-06550: line 9, column 6: PLS-00103: Encountered the symbol "=" when expecting
one of the following:
%
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
SQL*Plus command
set serveroutput ON;
clear screen;
PL/SQL Block
You need to have the Variable as sSql, also I corrected the extra single quote in the assignment!
Added block to CLOSE the cursor as well!
DECLARE
TYPE recPriceList is Record(comno varchar2(3),t$cpls varchar2(6),t$dsca varchar(100), Customers int,minExpiry varchar2(20),groupCount int,undefGroups int);
comno VARCHAR2(6) :='010';
sSql VARCHAR2(1000):=' ';
c sys_refcursor;
r recPricelist;
BEGIN
sSql := q'[select distinct lpad('010',3,'0'), t$cpls cpls,t$dsca,count(t$cuno),null Customers,null,null
from baan.ttccom010010 c
join baan.ttcmcs034010 p on c.t$cpls = p.t$cplt
where trim(t$cpls) is not null
group by T$CPLS,T$DSCA order by t$cpls]';
OPEN c FOR sSql ;
LOOP
FETCH c INTO r;
DBMS_OUTPUT.PUT_LINE(r.comno||' | '||r.t$cpls|| ' dsca='|| r.t$dsca);
EXIT WHEN c%notfound;
END LOOP;
CLOSE c;
END;
/
you assign a value for non-existing variable sql:
sql:=q'[select distinct lpad('010',3,'0'), t$cpls cpls,t$dsca,count(t$cuno),null Customers,null,null
from baan.ttccom010010 c
join baan.ttcmcs034010 p on c.t$cpls = p.t$cplt
where trim(t$cpls) is not null
group by T$CPLS,T$DSCA order by t$cpls']';
simply change the variable name to sSql

How do I run multiple scripts one after another in Oracle?

I have the following script:
ALTER TABLE ODANBIRM
ADD (OBID NUMBER(10, 0) );
----------------------------------------------------------------------------
CREATE OR REPLACE TRIGGER TR_OB_INC
BEFORE INSERT ON ODANBIRM
FOR EACH ROW
BEGIN
SELECT SEQ_OB.NEXTVAL INTO :NEW.OBID FROM DUAL;
END;
-----------------------------------------------------------------------------
DECLARE
CURSOR CUR IS
SELECT ROWID AS RID FROM ODANBIRM;
RC CUR%ROWTYPE;
BEGIN
OPEN CUR;
LOOP
FETCH CUR INTO RC;
EXIT WHEN CUR%NOTFOUND;
UPDATE ODANBIRM SET OBID=SEQ_OB.NEXTVAL WHERE ROWID=RC.RID;
END LOOP;
CLOSE CUR;
COMMIT;
END;
As you can see I have three different scripts (I've seperated them with dashed lines.) If I run this the first script runs but the second script, where I want to create a trigger fails saying "Encountered symbol "DECLARE"". If I take the trigger creation script away I get no error and the first and the last scripts run with no problem. What do I have to do to run them all without getting errors?
EDIT: I then realised that the second script should be just like this:
UPDATE ODANBIRM SET OBID=SEQ_OB.NEXTVAL;
So doing this simple thing in a loop is a waste of time and inefficient. I've once heard that one should use as much SQL and as less PL SQL as possible for efficiency. I think it's a good idea.
I think it's / on the line immediately following the end of a script. It's needed on ALL end of script lines for PL blocks, including the last. so...
However, do not put it on SQL statements; as it will run it twice (as Benoit points out in comments below!)
ALTER TABLE ODANBIRM
ADD (OBID NUMBER(10, 0) );
/
CREATE OR REPLACE TRIGGER TR_OB_INC
BEFORE INSERT ON ODANBIRM
FOR EACH ROW
BEGIN
SELECT SEQ_OB.NEXTVAL INTO :NEW.OBID FROM DUAL;
END;
/
DECLARE
CURSOR CUR IS
SELECT ROWID AS RID FROM ODANBIRM;
RC CUR%ROWTYPE;
BEGIN
OPEN CUR;
LOOP
FETCH CUR INTO RC;
EXIT WHEN CUR%NOTFOUND;
UPDATE ODANBIRM SET OBID=SEQ_OB.NEXTVAL WHERE ROWID=RC.RID;
END LOOP;
CLOSE CUR;
COMMIT;
END;
/
Shouldn't your anonymous block just be:
UPDATE ODANBIRM
SET OBID=SEQ_OB.NEXTVAL;

Resources