PLSQL - Error in associative array - oracle

Im trying to delete a set of tables and afterwards I want to recreate them using as select from. For couriousity I wanted to do this with an associative array. Unfortunately something is messed up, several errors appear and I can't find the reasons. This is the code:
DECLARE
TYPE t_tbl
IS
TABLE OF VARCHAR(100) INDEX BY VARCHAR2(100);
L_Tbl T_Tbl;
l_key VARCHAR2(100);
BEGIN
l_tbl('tableA') := 'table1';
l_tbl('tableB') := 'table2';
L_Tbl('tableC') := 'table3';
l_tbl('tableD') := 'table4';
l_key := l_tbl.first;
LOOP
BEGIN
EXIT WHEN L_Key IS NULL;
Dbms_Output.Put_Line('Dropping TABLE '|| L_Key ||);
EXECUTE Immediate 'DROP TABLE ' || L_Key;
dbms_output.put_line(l_key ||' '|| l_tbl(l_key));
-- Catch exception if table does not exist
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -942 THEN
Raise;
END IF;
EXECUTE IMMEDIATE 'create table schema1.' ||l_key||' as select * from schema2.'||l_tbl(l_key)||;
l_key := l_tbl.next(l_key);
END LOOP;
End;
END;
I get these errors:
ORA-06550: line 17, column 56:
PLS-00103: Encountered the symbol ")" 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 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>
<an alternatively-quoted SQL
ORA-06550: line 26, column 102:
PLS-00103: Encountered the symbol ";" 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 su
ORA-06550: line 29, column 6:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
loop
The symbol "loop" was substituted for ";" to continue.
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Could someone give me a hint please? Thank you in advance!

You have some unterminated append operations || on lines:
Dbms_Output.Put_Line('Dropping TABLE '|| L_Key ||);
And
EXECUTE IMMEDIATE 'create table schema1.' ||l_key||' as select * from schema2.'||l_tbl(l_key)||;
Get rid of the || at the end.
Also the way you are using LOOP is incorrect. Refer example:
while elem is not null loop
dbms_output.put_line(elem || ': ' || var_assoc_varchar(elem));
elem := var_assoc_varchar.next(elem);
end loop;

Remove || at the end of the line in line 17 and line 25.
And also ending loop before ending the block begin....end. get begin before loop or get end before end loop.
l_key := l_tbl.next(l_key);
END LOOP;
END;

Related

error PLSQL creating procedure with exception

I'm trying to create a procedure in PL/SQL and I receive the following error:
Errors: PROCEDURE BORRAR_PELI
Line/Col: 10/1 PLS-00103: Encountered the symbol "EXCEPTION" when expecting one of the following:
( begin case declare end exit for goto if loop mod null
pragma raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge
json_exists json_value json_query json_object json_array***
My procedure:
CREATE OR REPLACE PROCEDURE borrar_peli(delete_id peliculas.id%TYPE)
IS
corrupto EXCEPTION;
precio_dia_v peliculas.precio_dia%TYPE;
BEGIN
SELECT precio_dia INTO precio_dia_v FROM peliculas WHERE id = delete_id;
IF precio_dia_v > 4 THEN
RAISE corrupto;
ELSE
DELETE FROM pelĂ­culas WHERE id = delete_id;
COMMIT;
END IF;
EXCEPTION
WHEN corrupto THEN
DBMS_OUTPUT.put_line('es corrupto');
WHEN OTHERS THEN
DBMS_OUTPUT.put_line('Error code ' || SQLCODE || ': ' || SQLCODE);
END;

Stored Procedure + Call Not working

My Stored Procedure compiles, however; when I try to call it i get the following error:
Encountered the symbol "IS" 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 "IS" was ignored.
ORA-06550: line 2, column 48:
PLS-00103: Encountered the symbol "," when expecting one of the following:
:= ( ; not null range default character
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
My Stored Procedure is as follows:
create or replace procedure p_xml
(v_utc_offset in XML_HOURS_LOAD.UTCOFFSET%type
, v_data_date in XML_HOURS_LOAD.DATA_DATE%type
, v_data_type in XML_HOURS_LOAD.DATA_TYPE%type
, v_posted_value in XML_HOURS_LOAD.POSTEDVALUE%type
, v_utc_value in XML_HOURS_LOAD.UTCVALUE%type
, v_hour in XML_HOURS_LOAD.HOUR%type
, v_data_code in XML_HOURS_LOAD.DATA_CODE%type
)
AS
BEGIN
if v_utc_offset >= 4 THEN
INSERT INTO xml_hours_Load (UTCOffset, Data_date, Data_Type, PostedValue, UTCValue, Hour, Data_Code)
VALUES(v_utc_offset, v_data_date, v_data_type, v_posted_value, v_utc_value, v_hour, v_data_code);
COMMIT;
END IF;
END;
How I am calling it (and what is returning the error listed above) is as follows:
DECLARE
v_utc_offset is XML_HOURS_LOAD.UTCOFFSET%type,
v_data_date is XML_HOURS_LOAD.DATA_DATE%type,
v_data_type is XML_HOURS_LOAD.DATA_TYPE%type,
v_posted_value is XML_HOURS_LOAD.POSTEDVALUE%type,
v_utc_value is XML_HOURS_LOAD.UTCVALUE%type,
v_hour is XML_HOURS_LOAD.HOUR%type,
v_data_code is XML_HOURS_LOAD.DATA_CODE%type;
CURSOR cXmlHoursLoadCursor is (SELECT utcoffset, data_date, data_type, postedvalue, utcvalue, hour, data_code
from xml_hours_load);
BEGIN
FOR v in cXmlHoursLoadCursor LOOP
p_xml(v.utcoffset, v.data_date, v.data_type, v.postedvalue, v.utcvalue, v.hour, v.data_code);
COMMIT;
END LOOP;
END;
Thanks in Advance!
In your anonymous block, the CURSOR is the only variable you should declare using the IS keyword. Remove it from the others.
DECLARE
v_utc_offset XML_HOURS_LOAD.UTCOFFSET%type;
v_data_date XML_HOURS_LOAD.DATA_DATE%type;
v_data_type XML_HOURS_LOAD.DATA_TYPE%type;
v_posted_value XML_HOURS_LOAD.POSTEDVALUE%type;
v_utc_value XML_HOURS_LOAD.UTCVALUE%type;
v_hour XML_HOURS_LOAD.HOUR%type;
v_data_code XML_HOURS_LOAD.DATA_CODE%type;
CURSOR cXmlHoursLoadCursor is (SELECT utcoffset, data_date, data_type, postedvalue, utcvalue, hour, data_code
from xml_hours_load);
BEGIN
FOR v in cXmlHoursLoadCursor LOOP
p_xml(v.utcoffset, v.data_date, v.data_type, v.postedvalue, v.utcvalue, v.hour, v.data_code);
COMMIT;
END LOOP;
END;

Singleton Query Used In PL-SQL Condition (IF Statement)

I get the following error when trying to run my code:
BEGIN
IF((select count(*) from tablename where column = 'value') > 0) THEN
-- do stuff.
ELSE
-- do stuff.
END IF;
END;
Error:
ORA-06550: line 2, column 5:
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:
( - + case mod new not null <an identifier>
<a double-quoted delimited-identifier> <a bind variable>
continue avg count current exists max min prior sql stddev
sum variance execute forall merge 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>
Obviously the error code makes no sense. I'm getting used to this when working with Oracle.
The COUNT() function exists in PL. What else could it be.
Lance Link is that you under cover? You can't have a query in an IF statement. First do the query as a select INTO a variable, then test the variable:
DECLARE
v_count number;
BEGIN
select count(*)
into v_count
from tablename
where column = 'value';
IF ( v_count > 0) THEN
-- do stuff.
ELSE
-- do stuff.
END IF;
END;
Give Mata my regards.

What is the meaning of "(." and ".)" in PL/SQL?

I have found this PL/SQL code but I can't find in Oracle documentation to be valid:
CREATE OR REPLACE PROCEDURE hard_priv AS
BEGIN
HTP.htmlOpen;
HTP.headOpen;
HTP.title (.Account Information.);
HTP.headClose;
HTP.bodyOpen;
HTP.br;
HTP.print('User ID: ' ||
OWA_SEC.get_user_id || '');
HTP.print('User Password: ' ||
OWA_SEC.get_password || '');
HTP.br;
HTP.bodyClose;
HTP.htmlClose;
END hard_priv;
END uu_hr_pkg;
"(." and ".)" is not PLSQL syntax.
So it cannot compile.
This simple:
begin
dbms_output.put_line('this is the right syntax');
dbms_output.put_line(.this_dont_work.);
end;
/
Gives a compilation error like that:
SQL> #a
dbms_output.put_line(.this_dont_work.);
*
ERROR at line 3:
ORA-06550: line 3, column 26:
PLS-00103: Encountered the symbol "." when expecting one of the following:
( ) - + case mod new not null <an identifier>
<a double-quoted delimited-identifier> <a bind variable> table continue avg count current exists max min prior sql
stddev sum variance execute multiset the both leading
trailing forall merge year month day hour minute second
timezone_hour timezone_minute timezone_region timezone_abbr
time timestamp interval date
In the real world, you must replace them with '; your thing should be:
CREATE OR REPLACE PROCEDURE hard_priv AS
BEGIN
HTP.htmlOpen;
HTP.headOpen;
HTP.title ('Account Information');
HTP.headClose;
....

Compiler error when using DBMS_OUTPUT.PUT_LINE

I using oracle developer tools for viusal studio to develop my pl/sql program,however,I encounter a problem with this simple code
Here is the code
BEGIN
DBMS_OUTPUT.PUT_LINE('helloWorld');
END;
the debug info as follow:
**ERROR
ORA-06550: line 1, column 6:
PLS-00103: Encountered the symbol "" when expecting one of the following:
begin case declare exit for goto if loop mod null pragma
raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge
<a single-quoted SQL string> pipe
<an alternatively-quoted SQL string>
The symbol "" was ignored.
ORA-06550: line 2, column 36:
PLS-00103: Encountered the symbol "" 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
<an identifier> <a double-quoted delimited-id**
try:
DECLARE
v_var VARCHAR2(15) := 'helloworld';
BEGIN
DBMS_OUTPUT.PUT_LINE(v_var);
END;

Resources