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

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.

Related

How to let user enter a value in PlSql in Oracle live server?

Procedure definition :
CREATE OR REPLACE PROCEDURE DISPLAY_PRODUCT(numpr IN integer,info OUT produit%ROWTYPE)
IS
BEGIN
SELECT * INTO info FROM produit WHERE produit.numprod = numpr;
END;
/
The main program :
DECLARE
display produit%ROWTYPE;
product_number integer := &product_number ;
BEGIN
DISPLAY_PRODUCT(product_number,display);
DBMS_OUTPUT.PUT_LINE('numero : '||display.numprod || 'nom : '||display.nomprod);
END;
/
How can i enter a value for product_number varaible (& doesn't work ) ?
And i get errors :
Procedure created.
ORA-06550: line 3, column 31:
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>
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 specif
Unfortunately, as far as I know, the answer is simple: you can't do that.

PLSQL - Error in associative array

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;

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;
....

INSERT INTO within an Oracle stored procedure

In an Oracle stored procedure, can you have an INSERT INTO immediately after opening a cursor?
I get this message:
Error(9,4): PLS-00103: Encountered the symbol "INSERT" when expecting one of
the following: ( - + case mod new not null select with <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>
On the 3rd line of the following:
BEGIN
OPEN GRSREFVALUES FOR
--Select REFERENCEVALUES of Unprocessed Variations
INSERT INTO l_data (value)
SELECT REFERENCEVALUE

plsql stored procedure PLS-00103 :encounterd the symbol "SELECT"

I tried to created a stored procedure in Oracle 11G but i get this exception:
Error(7,18): 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> <an alternat
Error(9,65): PLS-00103: Encountered the symbol ")"
this is my code:
create or replace
procedure PM_LOG_IN_SP(user_name_ in nvarchar2, password_ in nvarchar2, res out int)
is
begin
IF user_name_ is not null and password_ is not null then
res := (SELECT count(*)
FROM HR.SYSTEM_USERS_TBL S
WHERE S.USER_NAME = user_name_ AND S.PSWD = password_;)
ELSE
RES :=0;
END IF;
end PM_LOG_IN_SP;
If you want to set variable from a query, you should use select into clause:
create or replace
procedure PM_LOG_IN_SP(user_name_ in nvarchar2,
password_ in nvarchar2,
res out int)
is
begin
IF user_name_ is not null and password_ is not null then
SELECT count(*)
into res
FROM HR.SYSTEM_USERS_TBL S
WHERE S.USER_NAME = user_name_ AND S.PSWD = password_;
ELSE
RES :=0;
END IF;
end PM_LOG_IN_SP;

Resources