How do I run PL/SQL code within SQLPlus? - plsql

I am trying to run the following code within SQLPlus:
exec lbacsys.sa_sysdba.create_policy(policy_name => 'ACCESS_LOCATIONS',
column_name => 'OLS_COLUMN',
default_options => 'READ_CONTROL,INSERT_CONTROL,UPDATE_CONTROL,DELETE_CONTROL,LABEL_DEFAULT,LABEL_UPDATE,CHECK_CONTROL,');
However, I'm getting the following error:
BEGIN lbacsys.sa_sysdba.create_policy(policy_name => 'ACCESS_LOCATIONS',; END;
*
ERROR at line 1:
ORA-06550: line 1, column 78:
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 specification>
<an alternatively
It seems to me to be something I'm doing wrong with the syntax. I'm just not sure what it is. Any help would be appreciated. Thanks :)

The EXEC statement takes a line of code and wraps it in a BEGIN/END block. In this case you want to split your call over several lines of code, so you'll probably find it easier to add the BEGIN/END yourself:
BEGIN
lbacsys.sa_sysdba.create_policy(policy_name => 'ACCESS_LOCATIONS',
column_name => 'OLS_COLUMN',
default_options =>
'READ_CONTROL,INSERT_CONTROL,'
|| 'UPDATE_CONTROL,DELETE_CONTROL,'
|| 'LABEL_DEFAULT,LABEL_UPDATE,CHECK_CONTROL,');
END;
/

Related

PLSQL declare date like result of query

I would like to set variable for date of first monday from sysdate and return this day in put_line but it's not working. I working on oracle database
SET serveroutput on;
DECLARE
first_monday DATE;
BEGIN
first_monday := select NEXT_DAY(sysdate, 'MONDAY') from dual;
DBMS_OUTPUT.PUT_LINE(first_monday);
END;
/
result should be 06-04-20
what is wrong?
my error below
ERROR at line 4:
ORA-06550: line 4, column 29:
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
ORA-06550: line 4, column 73:
PLS-00103: Encountered the symbol ")" when expecting one of the following:
. , # ; for <an identifier>
<a double-quoted delimited-identifier> group having intersect
minus order partition start subpartition union where connect
ORA-06550: line 8, column 0:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
end not pragma final instantiable order overriding static
member constructor map
You can directly call next_day in PL/SQL (no need for a query). This is desirable because it doesn't cause a context switch between the PL/SQL engine and the SQL engine. Most, but not all, scalar functions can be used this way.
If you need to use it as part of a query then INTO is used to send a query result value into a variable.
Here are both in action.
DECLARE
first_monday DATE;
BEGIN
first_monday := next_day(SYSDATE,
'MONDAY');
dbms_output.put_line(first_monday);
SELECT next_day(SYSDATE,
'MONDAY')
INTO first_monday
FROM dual;
dbms_output.put_line(first_monday);
END;
/
The way to assign the result of a single row query to a variable in PL/SQL is via the INTO keyword. You need to change your code as follows:
DECLARE
first_monday DATE;
BEGIN
select NEXT_DAY(sysdate, 'MONDAY')
INTO first_monday
from dual;
DBMS_OUTPUT.PUT_LINE(first_monday);
END;

ORA-06550 & PLS-00103 Errors when setting Oracle APEX item's default value

I am trying to set the default value of an item in Oracle APEX 4.2 by selecting the first value in a table having the specified VIDEO_ID. In the item, under the Default section I have set Default Value Type = PL/SQL EXPRESSION, and in the Default Value block I have entered
SELECT UNIQUE_ALLEGATION_ID
FROM (
SELECT UNIQUE_ALLEGATION_ID
FROM TBL_UNIQUE_ALLEGATION
WHERE VIDEO_ID = :P2_VIDEO_SELECT) A
WHERE ROWNUM <= 1
ORDER BY ROWNUM;
This code works just fine in my Oracle editor (if I replace :P2_VIDEO_SELECT with a value; and I am positive that :P2_VIDEO_SELECT is properly set).
However, when I run the page, I get the following error:
ORA-06550: line 1, column 43: 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
pipe
Remember that Apex attributes that accept SQL or PL/SQL fragments must be executed by the Apex at runtime, and that these must necessarily be embedded in wrapping code that must compile and execute at runtime.
So, for example, a source that is of type "PL/SQL Expression" will probably be executed in something like this:
declare
temp_var varchar2(4000);
begin
temp_var := (
SELECT UNIQUE_ALLEGATION_ID
FROM (
SELECT UNIQUE_ALLEGATION_ID
FROM TBL_UNIQUE_ALLEGATION
WHERE VIDEO_ID = :P2_VIDEO_SELECT) A
WHERE ROWNUM <= 1
ORDER BY ROWNUM
);
--use temp_var somewhere
end;
Obviously, the above code is not expected to work, which is why you're seeing an error like ORA-06550.

Encountered the symbol "." when expecting one of the following

create or replace procedure zivtest is
utl_mail.send_attach_varchar2 varchar2(100) :=utl_mail;
mail_subject varchar2(255) := 'zivtest'||sysdate;
mail_message varchar2(1000):='zivtest'
||'<br>'||'User count of all active users in all Realms';
recipients_list varchar2(255) :='user#user.com';
p_sender varchar2(50) := 'user#user.com';
data clob;
data_header varchar2(255);
--E_too_much_values exception;
begin
for item IN ( select decode(r.irl_id,1,'UPSEC',2,'PC',3,'CSM',6,'DMROAM',7,'WORKFLOW',8,'CSS_DEALER',10,'CCBATCH',14,'CSS',61,'COL',81,'CRM'
,82,'FIU',83,'OCM',84,'BOA',127,'SAPI') realm,
count (a.user_id) users
from sec_idm_user a , SEC_IDM_REALM_A r
where a.iu_rlid=r.irl_id
and a.iu_lock='N'
and a.iu_last_login >= sysdate -90
group by r.irl_id) LOOP
if data is not null then
utl_mail.send_attach_varchar2(
sender => p_sender,
recipients => recipients_list,
subject => mail_subject,
message => mail_message,
mime_type => 'text/html; charset=WINDOWS-1255',
attachment => data_header || data ,
att_filename => 'USER_COUNT.csv');
end if ;
END LOOP;
END;
How can I fix this?
I got the following error when I try to compile it:
PLS-00103: Encountered the symbol "." when expecting one of the following:
<an identifier> <a double-quoted delimited-identifier> current
You are seeing this error because second line
utl_mail.send_attach_varchar2 varchar2(100) :=utl_mail;
doesn't make sense. you are trying to declare variable named utl_mail.send_attach_varchar2.
What you need is to install utl_mail package, to be able to use procedure utl_mail.send_attach_varchar2

PLS-00103 while creating 2 packages PLSQL

im trying to create 2 package body's in plsql. This is my code:
SET SERVEROUTPUT ON
CREATE OR REPLACE PACKAGE p_locations
AS
FUNCTION f_distance(Lat1 IN NUMBER, Lon1 IN NUMBER, Lat2 IN NUMBER, Lon2 IN NUMBER, Radius IN NUMBER DEFAULT 6387.7) return number;
END p_locations;
/
CREATE OR REPLACE PACKAGE BODY p_locations
AS
FUNCTION f_distance (Lat1 IN NUMBER, Lon1 IN NUMBER, Lat2 IN NUMBER, Lon2 IN NUMBER, Radius IN NUMBER DEFAULT 6387.7)
RETURN NUMBER
IS
-- Convert degrees to radians
DegToRad NUMBER := 57.29577951;
BEGIN
RETURN(NVL(Radius,0) * ACOS((sin(NVL(Lat1,0) / DegToRad) * SIN(NVL(Lat2,0) / DegToRad)) +
(COS(NVL(Lat1,0) / DegToRad) * COS(NVL(Lat2,0) / DegToRad) *
COS(NVL(Lon2,0) / DegToRad - NVL(Lon1,0)/ DegToRad))));
END f_distance;
END p_locations;
/
CREATE OR REPLACE PACKAGE p_winkel
AS
FUNCTION changeOpeningstijd("id" IN number) RETURN boolean;
END p_winkel;
/
CREATE OR REPLACE PACKAGE BODY p_winkel
AS
FUNCTION changeOpeningstijd("id" IN number)
RETURN boolean
IS
dbms_output.put_line('dit is uitgevoerd');
return true;
END changeOpeningstijd;
END p_winkel;
When I run this I gat 3 times a PLS-00103 error. The first is on line 6,16 and says encountered the symbol "." 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 "<an identifier>" was substituted for "." to continue.
The strange thing is that when I comment out the 2nd package body everything works fine. Whilst the errors are at the beginning of the first package definition.
Am I doing something stupid wrong here, or can't you create two packages in one session, or what else is going on here, because I don't see any logic in these errors.
You're just missing the BEGIN keyword:
CREATE OR REPLACE PACKAGE BODY p_winkel
AS
FUNCTION changeOpeningstijd("id" IN number)
RETURN boolean
IS
BEGIN ---- this was missing
dbms_output.put_line('dit is uitgevoerd');
return true;
END changeOpeningstijd;
END p_winkel;
/
The line number in the PL/SQL error refers to the PL/SQL block (package, in this case) it is caused by; it isn't the line number in your combined script, as would be the case for a plain SQL error.
When you run this with run script you get three errors reported, not just the one you referred to; and the other two both mention begin:
Errors: check compiler log
6/16 PLS-00103: Encountered the symbol "." 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 "<an identifier>" was substituted for "." to continue.
8/3 PLS-00103: Encountered the symbol "END" 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 inserted before "END" to continue.
9/13 PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
begin end function pragma procedure
As Ben mentioned it's a good idea to add a show errors after each spec/body definition to highlight where an error is seen; but you can also query the user_errors view to see the errors related to each invalid object.

PLS-00103: ERROR.Encountered the symbol "BEGIN" when expecting one of the following: <an identifier> <a double-quoted delimited-identifier>

I create a type:
CREATE OR REPLACE TYPE NAGULIVE.DEPOSIT_DATA_TYPE AS OBJECT (
DEPOSIT_NO VARCHAR2 (16 Byte),
ACCT_NAME VARCHAR2 (128 Byte),
DEPOSIT_DT DATE,
EPOSIT_AMT NUMBER (16,2)
);
If I execute the function it will show error. I don't know where I'm wrong.
CREATE OR REPLACE FUNCTION NAGULIVE.GET_DEPOSIT_DETAIL (IN_F_DATE DATE,IN_T_DATE DATE)
RETURN(NAGULIVE.DEPOSIT_DATA_TYPE) AS
BEGIN
RETURN QUERY(SELECT D.DEPOSIT_NO, ACCT_NAME, DEPOSIT_DT, DEPOSIT_AMT
FROM DEPOSIT_ACINFO D
INNER JOIN ACT_MASTER A ON D.CUST_ID=A.CUST_ID
INNER JOIN DEPOSIT_SUB_ACINFO DS ON D.DEPOSIT_NO=DS.DEPOSIT_NO WHERE DS.DEPOSIT_DT>=IN_F_DATE AND DS.DEPOSIT_DT<=IN_F_DATE);
END;
/
The error is
2/7 PLS-00103: Encountered the symbol "(" when expecting one of the following:
<an identifier> <a double-quoted delimited-identifier> self
long double ref char time timestamp interval date binary
national character nchar
4/1 PLS-00103: Encountered the symbol "BEGIN" when expecting one of the following:
<an identifier> <a double-quoted delimited-identifier>
As little as you understand about PL/SQL I doubt you coded the above all by yourself. The error messages refers to the non existing function QUERY.
CREATE OR REPLACE FUNCTION NAGULIVE.GET_DEPOSIT_DETAIL (IN_F_DATE DATE,IN_T_DATE DATE)
RETURN(NAGULIVE.DEPOSIT_DATA_TYPE) AS
BEGIN
RETURN QUERY(SELECT D.DEPOSIT_NO, ACCT_NAME, DEPOSIT_DT, DEPOSIT_AMT
^^^^^
FROM DEPOSIT_ACINFO D
INNER JOIN ACT_MASTER A ON D.CUST_ID=A.CUST_ID
INNER JOIN DEPOSIT_SUB_ACINFO DS ON D.DEPOSIT_NO=DS.DEPOSIT_NO WHERE DS.DEPOSIT_DT>=IN_F_DATE AND DS.DEPOSIT_DT<=IN_F_DATE);
END;
/
The opening bracket indicates that you want to pass parameters to the function but there is no variable called SELECT etc. etc. This of course looks like a SELECT statement. You mention Postgres in your comments. I know nothing about postgres but if that is something you can do in postgres I can attest you that there is no such thing in Oracle PL/SQL.
I guess all this does not bring you further but I must agree with a_horse_with_no_name you must start at square one and start learning PL/SQL.
create or replace function nagulive.get_deposit_detail(
in_f_date date,
in_t_date date
) return deposit_data_type
as
v_deposit_data deposit_data_type;
begin
select deposit_data_type(d.deposit_no, acct_name, deposit_dt, deposit_amt)
into v_deposit_data
from deposit_acinfo d
inner join act_master a on d.cust_id=a.cust_id
inner join deposit_sub_acinfo ds on d.deposit_no=ds.deposit_no
where ds.deposit_dt>=in_f_date
and ds.deposit_dt<=in_f_date;
return v_deposit_data;
end;
/
Although SQL and PL/SQL work well together they are not completely seamless. Unlike PostgreSQL, A SQL statement cannot be used as a native PL/SQL expression. Instead, a variable must be declared, the statement must SELECT INTO that variable, and then the variable is returned.
Also there was a minor syntax problem with the return declaration - there should not be parentheses around the return type. This is different than the return statement, which does optionally allow parentheses.
Those both look like understandable mistakes to me, I'm not sure why this question is getting so much hate.

Resources