I am having issues with implementing a function. It seems like that the syntax is not getting write.
Here's my different versions of code. None of them seems to work.
CREATE OR REPLACE FUNCTION F1 (DTE IN VARCHAR(50))
RETURN VARCHAR(50) IS
B1 VARCHAR(50);
SELECT * INTO B1 FROM DUAL;
RETURN B1
END
Even the below template phrase isnt working
create or replace function compute()
2 return varchar2
3 is
4 begin
5 end;
6 /
Heres the error
PLS-00103: Encountered the symbol "END" when expecting one of the
following:
( begin case declare exit for goto if loop mod null pragma
raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
Maybe i am just missing some small thing.
Your code has multiple issues(see inline comments in the following code:
CREATE OR REPLACE FUNCTION F1 (DTE IN VARCHAR) -- size is not needed here
RETURN VARCHAR IS -- size is not needed here
B1 VARCHAR(50);
BEGIN -- begin keyword is needed here
SELECT <some_column> INTO B1 FROM DUAL; -- column name instead of *
RETURN B1; -- ; is needed here
END; -- ; is needed here
Well, your first code lacks a ';' at the end of the last 2 lines.
your second sample needs somethings between the begin and end:
create or replace function compute()
return varchar2
is
begin
return 'a';
end;
/
Related
This is the code:
CREATE PROCEDURE print_string(IN input_string VARCHAR(255))
BEGIN
DECLARE num_chars INT DEFAULT 0;
IF input_string IS NULL THEN
SET num_chars = 0;
ELSE
SET num_chars = CHAR_LENGTH(input_string);
END IF;
SELECT UPPER(input_string), num_chars;
END;
I get error:
PLS-00103: Encountered the symbol "IN" when expecting one of the following: <an identifier> <a double-quoted delimited-identifier>
current delete exists prior
Errors: check compiler log
How do I fix: current delete exists prior?
The immediate error is that you have the argument name and mode the wrong way around - it should be (input_string IN ... not (IN input_string .... But there are other problems:
Oracle recommends VARCHAR2 over VARCHAR.
arguments just have the data type, not a size (or precision/scale), so it should be (input_string IN VARCHAR2) not (input_string IN VARCHAR2(255).
you are missing the IS/AS keyword.
DECLARE comes before BEGIN in a PL/SQL block; having a nested block here would be valid, but you're missing a BEGIN and END; if you do that, and it isn't necessary so I don't think it's what you meant. And you don't need the DECLARE at all for a procedure, it's implied.
if you want a default value for a PL/SQL variable then assign it, rather than using DEFAULT. (You don't really need to do this here, as you always assign a value later anyway, but I'm sticking with your general approach.)
it's probably better to use native Oracle types, so NUMBER or PLS_INTEGER instead of INT.
assignment of values is with :=, not SET ... = ....
CHAR_LENGTH should just be LENGTH (unless you have your own function with that name).
in PL/SQL you have to select into something, and from something. But if you do that here, you still have to return it to the caller somehow.
given that you want to 'print' the string, you probably want dbms_output - though that relies on the client showing the result, which most don't by default, and it's generally only used for debugging...
So this would work:
CREATE PROCEDURE print_string(input_string IN VARCHAR2) AS
num_chars PLS_INTEGER := 0;
BEGIN
IF input_string IS NULL THEN
num_chars := 0;
ELSE
num_chars := LENGTH(input_string);
END IF;
DBMS_OUTPUT.PUT_LINE(UPPER(input_string) || ': ' || num_chars);
END;
/
BEGIN
DBMS_OUTPUT.ENABLE;
print_string('This is a test');
END;
/
1 rows affected
dbms_output:
THIS IS A TEST: 14
fiddle
But again, dbms_output isn't ideal. And it could be done much more simply (#Mto has shown one way), or without using PL/SQL at all.
You can fix the issues (listing in #Alex Poole's answer) and simplify the procedure to:
CREATE PROCEDURE print_string(
input_string IN VARCHAR2
)
IS
BEGIN
DBMS_OUTPUT.PUT_LINE(UPPER(input_string) || ', ' || COALESCE(LENGTH(input_string), 0));
END;
/
Then:
BEGIN
DBMS_OUTPUT.ENABLE;
print_string('This is a test');
print_string(NULL);
END;
/
Outputs:
THIS IS A TEST, 14
, 0
fiddle
The code syntax is incorrect here. It should be something like
CREATE OR REPLACE PROCEDURE print_string(input_string IN VARCHAR2)
IS
BEGIN
printf('local_time_greeting');
Having some trouble getting a stored procedure to run with SQL Developer. Below is an example patterned exactly like the first few lines, with variable names changed for security reasons.
CREATE OR REPLACE PROCEDURE redacted ( an_in_variable IN VARCHAR ) AS
these VARCHAR;
variables VARCHAR;
don_apostrophe_t INT;
matter INT;
BEGIN
DECLARE cursor_giving_me_trouble CURSOR FOR
SELECT something FROM db.table WHERE condition_1 = condition_2;
...
In the editor the SELECT word is red-wavy-lined, and when I try to run the code the output returns
PLS-00103: Encountered symbol "FOR" when expecting one of the following := . ( # % ; not null range default character
Any ideas?
You need to use IS rather than FOR, but you have the terms of the definition in the wrong order as well:
DECLARE
CURSOR cursor_giving_me_trouble IS
SELECT something FROM db.table WHERE condition_1 = condition_2;
db<>fiddle which still errors because of the illegal object names in your obfuscated code, but not from the syntax; and a more complete example.
It's also possible you're trying to use the construct:
FOR cursor_giving_me_trouble IN (
SELECT something FROM db.table WHERE condition_1 = condition_2
) LOOP
...
rather than a sub-block (a new declare, followed by begin/end) with references only to cursor within that. db<>fiddle.
To get you started:
create or replace procedure redacted
( an_in_variable in varchar2 )
as
these varchar2(123);
variables varchar2(456);
don_apostrophe_t integer;
matter integer;
cursor cursor_giving_me_trouble is
select 'Welcome to PL/SQL' as whatever from dual where 1=1;
begin
for r in cursor_giving_me_trouble loop
dbms_output.put_line(r.whatever);
end loop;
end;
However, you often don't need a separate cursor definition as there is this compact syntax:
create or replace procedure redacted
( an_in_variable in varchar2 )
as
these varchar2(123);
variables varchar2(456);
don_apostrophe_t integer;
matter integer;
begin
for r in (
select 'Welcome to PL/SQL' as whatever from dual where 1=1
)
loop
dbms_output.put_line(r.whatever);
end loop;
end;
Following is my Code to create the trigger as follows.
CREATE or REPLACE TRIGGER sms_trigger
AFTER INSERT ON student
FOR EACH ROW
ENABLE
DECLARE lclcmd CHAR(255);
DECLARE res VARCHAR(255);
BEGIN
SET lclcmd = CONCAT("php C:/xampp/htdocs/sample/sms_send.php");
SET res = sys_exec(lclcmd);
END;
After running above code getting the error like follows
1) Error(1,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.
2) Error(4,11): PLS-00103: Encountered the symbol "LCLCMD" when expecting
one of the following: transaction <a SQL statement>
Check the below, note that concat function is wrong you have add a string .. to concat.
Note you dont have to use declare more then once, also you dont need to add SET, and you have to put := when you want to assign values.
CREATE or REPLACE TRIGGER sms_trigger
AFTER INSERT ON student
FOR EACH ROW
ENABLE
DECLARE
lclcmd VARCHAR(255);
res VARCHAR(255);
BEGIN
lclcmd := CONCAT('php C:/xampp/htdocs/sample/sms_send.php','something');
res := sys_exec(lclcmd);
END;
/
PLS-00103: Encountered the symbol "&" when expecting one of the following...
code:
Declare
num number;
Begin
num := #
if num > 0 then
dbms_output.put_line(‘Hello’);
end if;
end;
/
I'm not sure why I'm getting this message when I'm not seeing anything wrong.
Oracle Live SQL is a tool for trying out SQL and PL/SQL but it doesn't support substitution variable syntax (&var.).
Instead, you can create tables, populate them with data, then run SQL or PL/SQL using them, e.g.:
create table inputs (num number);
insert into inputs values (10);
Declare
num number;
Begin
select num into num from inputs;
if num > 0 then
dbms_output.put_line('Hello');
end if;
end;
/
(p.s. I had to fix the quotes around 'Hello' for this to work)
Oracle live does not support & for get value.
Instead you can initialize the Variable
Declare
num number := 10;
Begin
if num > 0 then
dbms_output.put_line(‘Hello’);
end if;
end;
/
I have 5 to 6 tables in which i am inserting data using Excel file but i don't know whats the issue but for some data special character are adding and that is giving me problem. So is their any way to update complete table by passing the only Table_name in some procedure.currently i am using one function to update the column but i want something more easy to use (for my other team member).
I am using below function .
create or replace FUNCTION pfm_on_varchar(
p_str IN VARCHAR2)
RETURN VARCHAR2
IS
o_str VARCHAR2(4096) := '';
asc_val NUMBER;
BEGIN
FOR I IN 1 .. LENGTH(p_str)
LOOP
asc_val := ascii(SUBSTR(p_str,i,1));
IF ((asc_val BETWEEN 48 AND 57) OR (asc_val BETWEEN 65 AND 90) OR (asc_val BETWEEN 97 AND 122)) THEN
o_str := o_str || chr(asc_val);
END IF;
END LOOP;
RETURN o_str;
END pfm_on_varchar;
and how can I pass table name and column name in a procedure to update the table ?I am trying this code
CREATE OR REPLACE PROCEDURE removeSpecialChar(table_new IN varchar2, column_new IN varchar2)
AS
BEGIN
update table_new
set column_new = PFM_ON_VARCHAR(column_new);
end removeSpecialChar;
/
Error I am getting
Error(4,3): PL/SQL: SQL Statement ignored
Error(4,10): PL/SQL: ORA-00942: table or view does not exist
As mentioned in the comments , you can try using SQL as below:
Update table1
set columnname = case
when ascii(columnname) between 48 and 54
then replace (columnname,ascii(48),'')
end
where <condition> ;
update table
set columnName = regexp_replace('This is a test. $%&^*&* ', '[^A-Za-z .?!]', '')
Add all the characters which you don't want removed in the [^A-Za-z .]
For the procedure you could do a dynamic sql. Otherwise it will not pick the variable up. Like this:
CREATE OR REPLACE PROCEDURE removeSpecialChar(table_new IN varchar2, column_new IN varchar2)
AS
BEGIN
execute immediate 'update '||table_new||
' set '||column_new||' = PFM_ON_VARCHAR('||column_new||')';
end removeSpecialChar;
/