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

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;

Related

Error when declaring a number variable in a function block in Oracle SQL

SQL Worksheet
create or replace FUNCTION checkSubmitted (pStudentId IN VARCHAR, pCourseCode IN VARCHAR, pAssignmentNumber IN NUMBER)
RETURN VARCHAR
IS output VARCHAR(20);
DECLARE mark_result NUMBER;
BEGIN
SELECT assignment.mark INTO mark_result FROM Assignment WHERE student_id = pStudentId AND course_code = pCourseCode AND assignment_number = pAssignmentNumber;
IF (mark_result IS NOT NULL) THEN
RETURN 'Submitted';
ELSE
RETURN 'Not Submitted';
END IF;
END;
Compiler - Log
LINE/COL ERROR
--------- -------------------------------------------------------------
4/5 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 The symbol "begin" was substituted for "DECLARE" to continue.
12/4 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 <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
Errors: check compiler log
I want to create a SQL check function and i need to declare a variable in the function.
Please advise how can i achieve this.
As commented, your current problem is easy to fix.
However, I'd like to point you to another possible "mistake" - what will happen if there's no data for combination of those parameters' values? SELECT won't return NULL - it will raise NO_DATA_FOUND exception, and you should handle it. (I presume that it isn't likely for query to return more than a single value; if it is, handle it as well).
Also, try to avoid multiple RETURN statements. Procedures can be long and you might get lost in where you return and what you return. Set output variable's value (which you declared, but never used), and return it at the end of the procedure.
Therefore, consider something like this:
create or replace function checksubmitted
(pstudentid in varchar, pcoursecode in varchar, passignmentnumber in number)
return varchar
is
mark_result number;
output varchar(100);
begin
select a.mark
into mark_result
from assignment a
where a.student_id = pstudentid
and a.course_code = pcoursecode
and a.assignment_number = passignmentnumber;
if mark_result is not null then
output := 'Submitted';
else
output := 'Not Submitted';
end if;
return output;
exception
when no_data_found then
output := 'There is no data for these input parameters';
return output;
end;
/

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.

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.

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;

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

Resources