What is wrong with this PL/SQL [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 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

Related

I'm getting a 'Procedure created with compilation errors.'

I'm a beginner to sql and i'm getting a compilation error, can anyone pls guide me with my mistake in the code.
set serveroutput on;
create or replace procedure p1(n in varchar) as
cursor c1 is
select e_name, inv_amount
from employee, investment
where employee.e_id=investment.e_id
and inv_amount=50000;
c c1 %rowtype;
begin
open c1;
dbms_output.put_line('e_name'||"||'inv_amount');
loop
fetch c1 into c;
exit when c1 %notfound;
if(c.inv_amount=n)then
dbms_output.put_line(c.e_name||c.inv_amount);
end if;
end loop;
close c1;
end;
/
I can see some potential errors in your code. Which must be fixed as -
set serveroutput on;
create or replace procedure p1(n in varchar)
AS
cursor c1 is
select e_name,inv_amount
from employee,investment
where employee.e_id=investment.e_id
and inv_amount=50000;
c c1%rowtype; -- No space between cursor and ROWTYPE keyword
begin
open c1;
dbms_output.put_line('e_name'||''||'inv_amount'); -- It must be 2 single quotes instead of 1 double quotes
loop
fetch c1 into c;
exit when c1%notfound; -- Remove space
if(c.inv_amount=n)then
dbms_output.put_line(c.e_name||c.inv_amount);
end if;
end loop;
close c1;
end;
/
You can simplify this quite a lot:
You only care about rows where inv_amount = n, so why not add that as a filter condition in the cursor.
The Cursor FOR loop construction avoids the need to declare everything explicitly.
create or replace procedure p1
( n in number )
as
begin
for r in (
select e_name, inv_amount
from employee e
join investment i on i.e_id = e.e_id
where inv_amount = n
)
loop
dbms_output.put_line(r.e_name || ': ' || r.inv_amount);
end loop;
end p1;
It's also best to write joins using the join keyword.
Note that SQL is the query language and PL/SQL is the programming language, so this a PL/SQL procedure, not SQL.
Regarding "Procedure created with compilation errors", you need to get familiar with how to display and trace compilation errors using your development tool. In SQL*Plus, type
show errors
and you'll get something like this (using your original version of the procedure):
Warning: Procedure created with compilation errors.
SQL> show errors
Errors for PROCEDURE P1:
LINE/COL ERROR
---------- ---------------------------------------------------------------------------------------------------
10/34 PLS-00112: end-of-line in quoted identifier
10/35 PLS-00103: Encountered the symbol "||'inv_amount');" when expecting one of the following:
( - + case mod new null <an identifier>
<a double-quoted delimited-identifier> <a bind variable>
continue avg count current max min prior sql stddev sum
variance execute forall merge standard time timestamp
interval date
<a string literal with character set specification>
<a number> <a single-quoted SQL string> pipe
<an alternatively-quoted string literal with character set specification>
<
You can use the list command to print individual lines, for example line 10 mentioned in the compilation error:
SQL> l 10
10* dbms_output.put_line('e_name'||"||'inv_amount');
or list a range, e.g. lines 8-11:
SQL> l 8 11
8 begin
9 open c1;
10 dbms_output.put_line('e_name'||"||'inv_amount');
11* loop
If you are not using SQL*Plus, you can always query the user_errors dictionary view. However, developer tools like SQL Developer, PL/SQL Developer and Toad highlight compilation errors right in the editor so no special commands are needed.

ORA-06550 with alter statement [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 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.

Trying to assign a single value from a query to a variable... what am i doing wrong?

I am trying to to set a variable (v_flag_id) to the result of a query. I've been looking online at examples and it seems like my formatting/syntax is correct. What am i doing wrong? Thanks in advance.
create or replace PROCEDURE RUN_AGG
is
declare
v_Flag_id Number := select flag_id from flag where flag_tx = 'Processed / Calculated';
CURSOR hours IS
SELECT distinct(HR) as RHR
, submission_value_id
from (
select
v.DATA_DATE,
v.HR,
sv.submission_value_id
from value v
inner join submission_value sv on sv.value_id = v.value_id
where sv.SUBMISSION_VALUE_ID NOT IN (
SELECT SUBMISSION_VALUE_ID FROM VALUE_FLAG WHERE VALUE_FLAG.FLAG_ID = v_Flag_id
);
BEGIN
OPEN hours;
LOOP
FETCH hours into l_hr;
EXIT WHEN hours%NOTFOUND;
AGG_HOURLY_REG_FINAL(l_hr.RHR);
END LOOP;
CLOSE hours;
END RUN_AGG;
The error that I am receiving is as follows:
Error(6,1): PLS-00103: Encountered the symbol "DECLARE" when expecting one
of the following: begin function pragma procedure subtype type <an
identifier> <a double-quoted delimited-identifier> current cursor delete
exists prior external language
Use the following :
CREATE OR REPLACE PROCEDURE RUN_AGG IS
l_rhr VARCHAR2 (100);
l_sub_vl_id VARCHAR2 (100);
CURSOR hours is
SELECT distinct (HR) as RHR, submission_value_id
FROM (SELECT v.DATA_DATE, v.HR, sv.submission_value_id
FROM value_ v
INNER JOIN submission_value sv
ON (sv.value_id = v.value_id)
WHERE sv.SUBMISSION_VALUE_ID NOT IN
(SELECT SUBMISSION_VALUE_ID
FROM VALUE_FLAG
WHERE VALUE_FLAG.FLAG_ID in
(SELECT flag_id
FROM flag
WHERE flag_tx = 'Processed / Calculated')));
BEGIN
OPEN hours;
LOOP
FETCH hours INTO l_rhr, l_sub_vl_id;
EXIT WHEN hours%NOTFOUND;
AGG_HOURLY_REG_FINAL(l_rhr);
END LOOP;
CLOSE hours;
END RUN_AGG;
remove declare
take select flag_id into v_Flag_id from flag where flag_tx =
'Processed / Calculated'; sql in hours cursor's select. So, remove v_Flag_id variable.
return two variables for two columns l_rhr and l_sub_vl_id.
I replaced the name of the table value with value_, since it's a
reserved keyword for oracle.

Creating a anonymous block to display 1 to 5 and getting the below error

ORA-06550: line 2, column 11:
PLS-00103: Encountered the symbol "=" when expecting one of the following:
constant exception <an identifier>
<a double-quoted delimited-identifier> table long double ref
char time timestamp interval date binary national character
nchar
The symbol "<an identifier>" was substituted for "=" to continue.
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Here is the code:
SET SERVEROUTPUT ON;
clear scr;
DECLARE v_counter := 0;
BEGIN
LOOP
v_counter:= v_counter+1;
IF v_counter=3 THEN CONTINUE; END IF;
EXIT WHEN v_counter=5;
END LOOP;
DBMS_OUTPUT.PUT_LINE('v_counter='||v_counter);
END;
Creating a anonymous block to display 1 to 5 and getting the below error
Your code will never display 1 to 5. As #David said in the comments -
You haven't declared the data type for the variable.
The DBMS_OUTPUT is outside the LOOP, thus it will print only the last value held by the variable.
That IF-ELSE construct seems unnecessary if you just want to print 1 to 5.
You could achieve the same in just one FOR LOOP -
SQL> SET SERVEROUTPUT ON
SQL> BEGIN
2 FOR i IN 1..5
3 LOOP
4 DBMS_OUTPUT.PUT_LINE('v_counter='||i);
5 END LOOP;
6 END;
7 /
v_counter=1
v_counter=2
v_counter=3
v_counter=4
v_counter=5
PL/SQL procedure successfully completed.
SQL>
The same could be done in SQL -
SQL> select 'v_counter ='||level counter from dual connect by level <=5;
COUNTER
---------------------------------------------------
v_counter =1
v_counter =2
v_counter =3
v_counter =4
v_counter =5
SQL>
The problem is the line reading
DECLARE v_counter := 0;
You forgot to specify the variable's datatype.
It should read
DECLARE v_counter number := 0;
Of course, instead of number you might choose pls_integer if you're more confortable with it.

stored procedure always returns "Process exited" error always

I am working on a table structure similar to the below and trying to get the output as mentioned.
To acheive this, I written the below PL SQL procedure:
CREATE OR replace PROCEDURE Sample_procedure
AS
TYPE list_of_names_t
IS TABLE OF emp.emp_index%TYPE;
processedindexes LIST_OF_NAMES_T := List_of_names_t();
flag emp.emp_index%TYPE;
CURSOR c1 IS
SELECT *
FROM emp
WHERE Trim(emp_id) = 'AAAAA'
ORDER BY last_maint_ts ASC;
BEGIN
dbms_output.Put('Entered the loop');
FOR rec IN c1 LOOP
SELECT emp_index
INTO flag
FROM emp
WHERE emp_id = rec.emp_id
AND last_maint_ts > rec.last_maint_ts;
IF flag IS NOT NULL THEN
processedindexes.extend;
Processedindexes(processedindexes.last) := flag;
processedindexes.extend;
Processedindexes(processedindexes.last) := rec.emp_index;
dbms_output.Put('The indexes'
||rec.emp_index
||' & '
||flag
||'refer to same emp ID');
exit;
dbms_output.Put('received NULL');
END IF;
END LOOP;
END;
1) Everytime, I run this i get the output as Process exited in sql developer, Any suggestion on this?
Note: I am new to PL SQL programming, please correct me if my approach of solving this is not right.
open sqlplus and execute the following :-
set serveroutput on
exec Sample_procedure;
provided your procedure is compiling fine, it should display the output in dbms_output.put

Resources