I am doing an assignment, and am trying to get this to work:
declare sal number;
begin
sal := 1;
select min_salary into sal from jobs where job_ID = 'ad_vp';
dbms_output put_line('value is' || sal);
end;
If this was mssql, i'd never be asking this, but my studies uses oracle for this semester. Why do I get this error message, and how do I avoid it?
> ORA-06550: line 5, column 13: PLS-00103: Encountered the symbol
> "PUT_LINE" when expecting one of the following:
>
> := . ( # % ; The symbol ":=" was substituted for "PUT_LINE" to
> continue.
> 3. sal := 1;
> 4. select min_salary into sal from jobs where job_ID = 'ad_vp';
> 5. dbms_output put_line('value is' || sal);
> 6. end;
You are missing a dot, it should be:
dbms_output.put_line('value is' || sal);
Related
I am new to PLSQL. Please help find the solution of the below mentioned error.
Error is
Error report -
ORA-06550: line 8, column 1:
PLS-00103: Encountered the symbol "BEGIN" 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:
**The code I executed is
**
DECLARE
c_id customers.ID%type :=1 ;
c_name customers.NAME%type ;
c_age customers.AGE%type ;
c_addr customers.ADDRESS%type ;
c_sal customers.SALARY%type
BEGIN
SELECT NAME, AGE, ADDRESS, SALARY INTO c_name, c_age, c_addr, c_sal FROM CUSTOMERS
WHERE ID = c_id ;
dbms_output.put_line('Customer' || c_name || ' from ' || c_addr || 'earns' || c_sal ) ;
END ;
/
OUTPUT SHOULD BE
Customer Ramesh from Ahmedabad earns 2000
Error says it all: "Encountered the symbol "BEGIN" when expecting ". That implies there is a syntax error before the BEGIN keyword. So look at the lines above and observe the line above does not have a terminating semi colon.
Try to do some basic debugging. Takes less time than posting the question here ;)
DECLARE
c_id customers.ID%type :=1 ;
c_name customers.NAME%type ;
c_age customers.AGE%type ;
c_addr customers.ADDRESS%type ;
c_sal customers.SALARY%type ; <<< missing semi colon
BEGIN
SELECT NAME, AGE, ADDRESS, SALARY INTO c_name, c_age, c_addr, c_sal FROM CUSTOMERS
WHERE ID = c_id ;
dbms_output.put_line('Customer' || c_name || ' from ' || c_addr || 'earns' || c_sal ) ;
END ;
/
#C:\Users\4\Desktop\dbdrop;
#C:\Users\4\Desktop\dbcreate;
SET SERVEROUTPUT ON;
DECLARE
ORDER_ID ORDERS.ODID%TYPE;
COMPANY_NAME ORDERS.CNAME%TYPE;
ORDER_DATE ORDERS.ODATE%TYPE;
CURSOR ord_cursor IS
SELECT ODID, CNAME, ODATE
FROM ORDERS
WHERE ODER_DATE< TRUNC(SYSDATE);
FETCH FIRST 5 ROWS ONLY;
BEGIN
OPEN ord_cursor;
LOOP
FETCH ord_cursor into ORDER_ID, COMPANY_NAME, ORDER_DATE;
DBMS_OUTPUT.PUT_LINE(' ');
DBMS_OUTPUT.PUT_LINE('ODER ID: '|| TO_CHAR(Order_Id));
DBMS_OUTPUT.PUT_LINE( 'ODER DATE: ' || ORDER_DATE );
DBMS_OUTPUT.PUT_LINE('COMPANY NAME: '|| COMPANY_NAME );
DBMS_OUTPUT.PUT_LINE( '------------');
DBMS_OUTPUT.PUT_LINE( '------------');
IF ord_cursor%NOTFOUND THEN
EXIT;
END IF;
END LOOP;
CLOSE ord_cursor;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
Error report -
ORA-06550: line 9, column 12:
PLS-00103: Encountered the symbol "FETCH" when expecting one of the following:
begin function pragma procedure subtype type
current cursor delete
exists prior
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
This:
WHERE ODER_DATE< TRUNC(SYSDATE);
FETCH FIRST 5 ROWS ONLY;
is wrong; either remove semi-colon in the first line (if your database supports FETCH clause), or entire second line.
Shorter version of your code is something like this (I don't have your tables so I fabricated one, based on Scott's EMP table):
SQL> create table orders as
2 select empno ordid, ename cname, hiredate odate
3 from emp
4 where deptno = 10;
Table created.
Code itself:
SQL> set serveroutput on
SQL> begin
2 for cur_r in
3 (select ordid, cname, odate
4 from orders
5 where odate < trunc(sysdate)
6 and rownum <= 5
7 )
8 loop
9 dbms_output.put_line('------------');
10 dbms_output.put_line('Order ID = ' || cur_r.ordid);
11 dbms_output.put_line('Order date = ' || to_char(cur_r.odate, 'dd.mm.yyyy'));
12 dbms_output.put_line('Company = ' || cur_r.cname);
13 end loop;
14 end;
15 /
------------
Order ID = 7782
Order date = 09.06.1981
Company = CLARK
------------
Order ID = 7839
Order date = 17.11.1981
Company = KING
------------
Order ID = 7934
Order date = 23.01.1982
Company = MILLER
PL/SQL procedure successfully completed.
SQL>
Your query ends before the FETCH clause and that is the issue.
You can use simple FOR loop and string concatenation as follows:
BEGIN
FOR CUR IN (SELECT 'ODER ID: '|| TO_CHAR(ODID) || CHR(10)
|| 'ODER DATE: ' || ODATE || CHR(10)
|| 'COMPANY NAME: ' || CNAME || CHR(10)
|| '------------' AS STR
FROM ORDERS
WHERE ODER_DATE< TRUNC(SYSDATE)
FETCH FIRST 5 ROWS ONLY)
LOOP
DBMS_OUTPUT.PUT_LINE(CUR.STR);
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
/
can someone let me know where is an issue in my code?
SET SERVEROUTPUT ON;
daclare
sql_stmt varchar(500);
n number(3);
n :=0;
begin
FOR vori IN (select ri from voucher_p1 where vchstate=chr(4))
LOOP
sql_stmt := 'select count(sernum) from voucher_p1 where ENCPIN in ( select ENCPIN from voucher_p1 where ri=' || vori.ri || ')'
EXECUTE immediate sql_stmt into n;
DBMS_OUTPUT.PUT_LINE( 'Column Variable: ' || n );
END LOOP;
end;
/
i run it in oracle 9.2.0 and it retuns this message as below:
EXECUTE immediate sql_stmt into n;
*
ERROR at line 5:
ORA-06550: line 5, column 1:
PLS-00103: Encountered the symbol "EXECUTE" when expecting one of the
following:
. ( * # % & = - + ; < / > at in is mod not rem
<an exponent (**)> <> or != or ~= >= <= <> and or like
between ||
i Run ok. thank everyone.
SET SERVEROUTPUT ON;
declare
sql_stmt varchar2(500);
n number(3);
begin
FOR vori IN (select ri from voucher_p1 where vchstate=chr(4))
LOOP
DBMS_OUTPUT.PUT_LINE('select count(*) from voucher_p1 where ENCPIN in ( select ENCPIN from voucher_p1 where ri=' || vori.ri || ')');
sql_stmt := 'select count(*) from voucher_p1 where ENCPIN in ( select ENCPIN from voucher_p1 where ri=' || vori.ri || ')';
EXECUTE immediate sql_stmt into n;
DBMS_OUTPUT.PUT_LINE(n);
END LOOP;
end;
/
i am trying to execute or run the following pl/sql script:
SET serveroutput on;
CREATE OR REPLACE PROCEDURE findAvg
(p_category IN products.category_id% TYPE, c OUT NUMBER)
AS
BEGIN
SELECT NVL ((SELECT AVG(LIST_PRICE) FROM products
WHERE p_category = category_id), -1) into p_category
from dual;
END findAvg;
DECLARE
cat products.category_id%TYPE;
Price products.List_price%TYPE;
BEGIN
cat := &p_category;
findAvg (cat, price);
if (price = -1) then
dbms_output.put_line('Wrong Category ');
ELSE
dbms_output.put_line('the average price for category' || cat || ' is ' || price);
END IF;
END;
/
show errors
but when i try to run it, i get this error message (i can see it only after show errors):
PLS-00103: Encountered the symbol "DECLARE"
what is wrong with this declare?
You are missing a "/" between the creation of the procedure and the start of the anonymous block that runs it:
SET serveroutput on;
CREATE OR REPLACE PROCEDURE findAvg
(p_category IN products.category_id% TYPE, c OUT NUMBER)
AS
BEGIN
SELECT NVL(AVG(LIST_PRICE),-1)
INTO c
FROM products
WHERE p_category = category_id;
END findAvg;
/
show errors
DECLARE
cat products.category_id%TYPE;
Price products.List_price%TYPE;
BEGIN
cat := &p_category;
findAvg (cat, price);
if (price = -1) then
dbms_output.put_line('Wrong Category ');
ELSE
dbms_output.put_line('the average price for category' || cat || ' is ' || price);
END IF;
END;
/
Also, the "show errors" command should be run just after creating the procedure, as I have above, and the INTO clause should specify the OUT parameter.
I have provided this code for fetching out all the table descriptions of all schemas in one database. I searched online and came up with the code below. I can't figure out the error.
Anyone who can either help with this error OR provide me an alternative to the problem
SQL> Begin
2 For q in (select distinct owner from dba_objects)
3 loop
4 for r in (select table_name, owner from all_tables where owner = 'q.owner')
5 loop
6 dbms_output.put_Line('table '||r.table_name);
7 execute immediate 'desc ' || r.table_name
8 end loop;
9 end loop;
10 end;
11 /
end loop;
*
ERROR at line 8:
ORA-06550: line 8, column 3:
PLS-00103: Encountered the symbol "END" when expecting one of the following:
. ( * # % & = - + ; < / > at in is mod remainder not rem
return returning <an exponent (**)> <> or != or ~= >= <= <>
and or like like2 like4 likec between into using || bulk
member submultiset
The symbol ";" was substituted for "END" to continue.
Try:
select *
from all_tab_columns
where owner = 'MY_OWNER_NAME'
order by owner,
table_name,
column_id;
Use the reference here to select the column of interest to you. You can add other predicates if you're only interested in character data types, for example.
Line 7: execute immediate 'desc ' || r.table_name
You forgot to put ;