PLS-00103: Encountered the symbol "BEGIN" when expecting one of the following: - oracle

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

Related

PL/SQL: Procedures Successful with compilation error

I am trying to create a procedure to dump out specific data from few tables:
CREATE OR REPLACE PROCEDURE Lab9_View IS
--declare the variable
o_Id orders.OrderID%type;
o_date orders.orderdate%type;
s_date orders.shippeddate%type;
c_name customers.companyname%type;
s_country orders.shippeddate%type;
--define CURSOR for select
CURSOR Lab9_View IS
SELECT OrderId, OrderDate, ShippedDate, CompanyName, ShipCountry
FROM Orders JOIN Customers ON
customers.customerid = orders.CustomerId WHERE ORDERDATE BETWEEN '2019-08-14' AND '2019-08-23';
--exceution begin here
BEGIN
--heading line
dbms_output.put_line(' Harry''s Lab9 - Orders by COuntry Aug. 14th-23rd ');
dbms_output.put_line(' Order #' || ' '|| ' Order Date' || ' ' || 'Shipped Date' || " " || 'Company Name' || ' ' || 'Ship Country');
--execute the select
OPEN Lab9_View;
--retrieve each row from result in loop
LOOP
FETCH Lab9_View into o_id, o_date, s_date, c_name, s_country;
--end of result reach
EXIT WHEN Lab9_View%notfound;
--dump out data
dbms_output.put_line(o_id || ' '|| o_date || ' ' || s_date || " " || c_name || ' ' || s_country);
END LOOP;
--close cursor
CLOSE Lab9_View;
END;
I keep on getting this error:
ORA-24344: success with compilation error
ORA-06512: at "SYS.WWV_DBMS_SQL_APEX_200100", line 581
ORA-06512: at "SYS.DBMS_SYS_SQL", line 1658
ORA-06512: at "SYS.WWV_DBMS_SQL_APEX_200100", line 567
ORA-06512: at "APEX_200100.WWV_FLOW_DYNAMIC_EXEC", line 2127
3. o_Id orders.OrderID%type;
4. o_date orders.orderdate%type;
5. s_date orders.shippeddate%type;
6. c_name customers.companyname%type;
7. s_country orders.shippeddate%type;
I can't spot anything different from my professor's lab notes. Can anyone see anything wrong with my code?
Thank you
You have double quotes instead of single quotes between two of the columns in your put_line commands.

ORA-06550: Encountered "end-of-file" when expecting the following: . ( % ; The query runs fine, but is giving errors in the block

create or replace procedure personWithGivenCity(city1 in varchar) as
cursor c1 is select p.name
from person p
join address ad
on p.name=ad.name
where ad.city=city1;
c c1%rowtype;
begin
open c1;
loop
fetch c1 into c;
exit when c1%notfound;
dbms_output.put_line(c.name);
end loop;
close c1;
end;
/
-- Block which class the procedure
declare
city varchar2(10):='Vasco';
begin
personWithGivenCity(city);
end;
/
The query works fine without the block. But I'm getting end-of-file error at line 3 column 11.
Table structures
Person
name varchar2(10) primary key,
gender varchar2(10),
age number
Address
name varchar2(10) references person(name),
hno number,
laneno number,
city varchar2(10),
state varchar2(10)
The error
ORA-06550: line 3, column 11: PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following: . ( % ; for

How to store mutiple column output in a for loop

Designing a function to show column_name and count of null values
Expected o/p
Col_name,total count
abc 45
def 30
fgh 10
enter code here
CREATE OR REPLACE FUNCTION gtr_ops.f_column_validation()
RETURNS TABLE
(
column_name character varying(50),
total_blank_records bigint
)
AS
$BODY$
DECLARE
i record;
ecode CHARACTER VARYING(1000);
vsql text;
BEGIN
raise notice 'entering for loop';
for i IN (select column_name from information_schema.columns where table_schema='xyz'
and table_name='xyz')
LOOP
--RAISE NOTICE '%', i.column_name;
column_name := i.column_name;
vsql := 'select count(*) from schema.tablename
where '|| i.column_name||' is null
group by '|| i.column_name;
RAISE NOTICE '%', vsql;
EXECUTE vsql into total_blank_records;
RETURN NEXT;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
ecode := SQLSTATE||' Error Message:'|| SQLERRM;
raise notice 'Err Msg: %',ecode;
END;
$BODY$
LANGUAGE plpgsql VOLATILE;
Error
Err Msg: 42703 Error Message:record "i" has no field "column_name"
Need guidance to solve the error
Well, I have tried your code and got a bit different error:
Err Msg: 42702 Error Message:column reference "column_name" is ambiguous
Indeed, you use column_name in return type and in select (select column_name from...). When I changed to select columns.column_name from... it worked.
But then, you have to replace schema.tablename with proper value (just like you do with i.column_name):
First:
for i IN (select columns.table_schema, columns.table_name, columns.column_name from information_schema.columns where ...
Then:
vsql := 'select count(*) from ' || i.table_schema || '.' || i.table_name || '
But remember, that you have hard-coded table_schema and table_name to be xyz.xyz in your query in for loop.
I was testing with PostgreSQL 9.5.3.

How to write dynamic sql in Oracle Stored procedure?

Basically in my update sql query column names going to be dynamic like
update bi_employee set <this_is_dynamic_column> where emp_id = 12
Below is the stored procedure that I have written so far.
CREATE OR replace PROCEDURE Sp_run_employee_updates
IS
CURSOR c_emp IS
SELECT *
FROM BI_EMPLOYEE_UPDATE
WHERE EFFECTIVE_DATE = To_date('30-Apr-2012', 'dd-mm-yy');
BEGIN
FOR employee_update IN c_emp LOOP
declare update_sql varchar2(225);
update_sql := 'UPDATE BI_EMPLOYEE SET '
|| employee_update.column_name
|| '= employee_update.new_value WHERE emp_id = '
|| employee_update.employee_id;
END LOOP;
END;
Its giving me foloowing errors
Error(17,13): PLS-00103: Encountered the symbol "=" when expecting one of the following: constant exception table long double ref char time timestamp interval date binary national character nchar The symbol "" was substituted for "=" to continue.
Error(22,5): PLS-00103: Encountered the symbol "UPDATE" when expecting one of the following: begin function pragma procedure subtype type current cursor delete exists prior The symbol "begin" was substituted for "UPDATE" to continue.
Error(31,6): PLS-00103: Encountered the symbol ";" when expecting one of the following: loop
a- This should be like this:
to_date('30-Apr-2012','dd-mon-yyyy');
b- You can do it like this:
CREATE OR REPLACE
PROCEDURE SP_RUN_EMPLOYEE_UPDATES IS
update_sql varchar2(225);
CURSOR c_emp IS
SELECT *
FROM BI_EMPLOYEE_UPDATE
WHERE EFFECTIVE_DATE = to_date('30-Apr-2012','dd-mon-yyyy');
BEGIN
FOR employee_update in c_emp LOOP
update_sql := 'UPDATE BI_EMPLOYEE SET ' || employee_update.column_name ||
'= :1 WHERE emp_id = :2' ;
execute immediate update_sql using employee_update.new_value, employee_update.employee_id;
END LOOP;
END SP_RUN_EMPLOYEE_UPDATES;

Syntax error on put_line

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

Resources