trying to implement dynamic sql via function call - plsql

I am recently implementing the dynamic sql in my cide and quite new to the concept. I was trying with the following function that will take the column name and update the value via dynamic function call.However the function gives out an error while compiling.Please find the code as below:
function upd_tab(col_name in varchar2,val in number)
return pls_integer
is
BEGIN
EXECUTE IMMEDIATE 'UPDATE EMPLOYEE1 SET '||col_name||'= :THE_VALUE WHERE EMP_NAME IN(:NAME1,:Nme2)'
using val,john,aaron;
RETURN SQL%ROWCOUNT;
END;
Thank you in advance for your help.

Here you have to declare variables in the using clause. Hope below snippet helps.
FUNCTION upd_tab(
col_name IN VARCHAR2,
val IN NUMBER)
RETURN pls_integer
IS
BEGIN
EXECUTE IMMEDIATE 'UPDATE EMPLOYEE1 SET '||col_name||'= :THE_VALUE WHERE EMP_NAME IN(:NAME1,:Nme2)' USING val,'john','aaron';
RETURN SQL%ROWCOUNT;
END;

Related

How to pass bind variable as IN OUT parameter in PLSQL procedure

I want to use 1st parameter of my procedure EMP_ID as IN OUT parameter. Originally it's IN parameter and this procedure is working fine, but as the last line concern
htp.p('Inserted for Employee-id '||EMP_ID);
I want to use this line in anonymous block and most importantly it should be a bind variable because I am creating the REST API in which user will only enter values and it will be taken as bind variable in oracle Apex and the below procedure is working fine with respect of IN parameter.
create or replace procedure att_time_ins (EMP_ID in varchar2, ORG_ID in number,V_TIME_STATUS in number) is
BEGIN
INSERT INTO TIME_ATTENDANCE_POOL
(EMPLOYEE_ID, ATTENDANCE_DATE,TIME_HOURS,TIME_MINUTES,TIME_STATUS,LOCATION_ID,ORG_ID,PREPARED_ON )
VALUES
(EMP_ID, to_date(sysdate,'DD/MM/YYYY'),to_char(sysdate,'HH24') ,to_char(sysdate,'MI'),V_TIME_STATUS,null,ORG_ID,
to_date(sysdate,'DD/MM/YYYY') );
COMMIT;
time_management.create_attendance_sheet(v_org_id => ORG_ID,
v_employee_id => EMP_ID,
target_date => to_date(sysdate,'DD/MM/YYYY'));
htp.p('Inserted for Employee-id '||EMP_ID);
end att_time_ins;
I am calling my procedure in this way
begin
att_time_ins(:employee_id,:org_id,:time_status);
end;
Please help me to modify this stuff according to IN OUT Parameter i.e Employee_id should be IN OUT parameter. There is no proper documentation regarding passing bind variables as in out prameter in PLSQL Block.
Let us say you have a procedure named PR_PROC, you can use VARIABLE statement for passing IN OUT or OUT kind of variables.
CREATE OR REPLACE PROCEDURE PR_PROC (EMP_NAME IN VARCHAR2,
EMP_ID IN OUT VARCHAR2)
IS
BEGIN
DBMS_OUTPUT.PUT_LINE (EMP_NAME||EMP_ID);
END;
VARIABLE KURSOR VARCHAR2
BEGIN
:KURSOR:='4';
PR_PROC('SENIOR',:KURSOR);
END;
Note: If you are using TOAD Editor you can press F5 to make it work.
Oracle VARIABLE

Update statement works in normal mode, but not in procedure

I want to upload image from directory to database blob field. For those reason I write this code. Code alone works well but is not working as a procedure.
What is the problem ? I cannot understand. Here is the code:
DECLARE
dest_loc BLOB;
src_loc BFILE;
BEGIN
src_loc:= BFILENAME('ALL_IMG_DIR','SDFGASDF1544.jpg');
DBMS_LOB.FILEOPEN(src_loc);
DBMS_LOB.CREATETEMPORARY(dest_loc,true);
DBMS_LOB.LOADFROMFILE(dest_lob => dest_loc, src_lob => src_loc,amount=>dbms_lob.getlength(src_loc) );
UPDATE STUDENT
SET IMAGE=dest_loc
WHERE
REG_CODE = 'SDFGASDF1544';
DBMS_LOB.CLOSE(src_loc);
end;
But when I write this code as a procedure, like
CREATE OR REPLACE PROCEDURE img_to_blob_student(Vreg_code varchar2)
is
dest_loc BLOB;
src_loc BFILE;
BEGIN
src_loc := BFILENAME('ALL_IMG_DIR','SDFGASDF1544.jpg');
DBMS_LOB.FILEOPEN(src_loc);
DBMS_LOB.CREATETEMPORARY(dest_loc,true);
DBMS_LOB.LOADFROMFILE(
dest_lob => dest_loc,
src_lob => src_loc,
amount=>dbms_lob.getlength(src_loc)
);
UPDATE STUDENT
SET IMAGE=dest_loc
WHERE REG_CODE = 'SDFGASDF1544';
DBMS_LOB.CLOSE(src_loc);
end;
And call like
img_to_blob_student('123');
I get
ERROR IS: `ORA-00900: invalid SQL statement in procedure`
to call the procedure, did you use the execstatement?
exec img_to_blob_student('123');

PL/SQL variable scope in nested blocks

I need to run some SQL blocks to test them, is there an online app where I can insert the code and see what outcome it triggers?
Thanks a lot!
More specific question below:
<<block1>>
DECLARE
var NUMBER;
BEGIN
var := 3;
DBMS_OUTPUT.PUT_LINE(var);
<<block2>>
DECLARE
var NUMBER;
BEGIN
var := 200;
DBMS_OUTPUT.PUT_LINE(block1.var);
END block2;
DBMS_OUTPUT.PUT_LINE(var);
END block1;
Is the output:
3
3
200
or is it:
3
3
3
I read that the variable's value is the value received in the most recent block so is the second answer the good one? I'd love to test these online somewhere if there is a possibility.
Also, is <<block2>> really the correct way to name a block??
Later edit:
I tried this with SQL Fiddle, but I get a "Please build schema" error message:
Thank you very much, Dave! Any idea why this happens?
create table log_table
( message varchar2(200)
)
<<block1>>
DECLARE
var NUMBER;
BEGIN
var := 3;
insert into log_table(message) values (var)
select * from log_table
<<block2>>
DECLARE
var NUMBER;
BEGIN
var := 200;
insert into log_table(message) values (block1.var || ' 2nd')
select * from log_table
END block2;
insert into log_table(message) values (var || ' 3rd')
select * from log_table
END block1;
In answer to your three questions.
You can use SQL Fiddle with Oracle 11g R2: http://www.sqlfiddle.com/#!4. However, this does not allow you to use dbms_output. You will have to insert into / select from tables to see the results of your PL/SQL scripts.
The answer is 3 3 3. Once the inner block is END-ed the variables no longer exist/have scope. You cannot access them any further.
The block naming is correct, however, you aren't required to name blocks, they can be completely anonymous.
EDIT:
So after playing with SQL Fiddle a bit, it seems like it doesn't actually support named blocks (although I have an actual Oracle database to confirm what I said earlier).
You can, however, basically demonstrate the way variable scope works using stored procedures and inner procedures (which are incidentally two very important PL/SQL features).
Before I get to that, I noticed three issues with you code:
You need to terminate the insert statements with a semi-colon.
You need to commit the the transactions after the third insert.
In PL/SQL you can't simply do a select statement and get a result, you need to select into some variable. This would be a simple change, but because we can't use dbms_output to view the variable it doesn't help us. Instead do the inserts, then commit and afterwards select from the table.
In the left hand pane of SQL Fiddle set the query terminator to '//' then paste in the below and 'build schema':
create table log_table
( message varchar2(200)
)
//
create or replace procedure proc1 as
var NUMBER;
procedure proc2 as
var number;
begin
var := 200;
insert into log_table(message) values (proc1.var || ' 2nd');
end;
begin
var := 3;
insert into log_table(message) values (var || ' 1st');
proc2;
insert into log_table(message) values (var || ' 3rd');
commit;
end;
//
begin
proc1;
end;
//
Then in the right hand panel run this SQL:
select * from log_table
You can see that proc2.var has no scope outside of proc2. Furthermore, if you were to explicitly try to utilize proc2.var outside of proc2 you would raise an exception because it is out-of-scope.

How to call a function inside a cursor in pl/sql?

I have declared a function which is returning some string as
function(varchar2) return varchar2;
I want this function to be called from inside of a cursor like
open p-cursor for select * from employee where emp_name = function(ssn).
But it throws some error.
The query being used in cursor is working fine when executed separately.
When you say you have "declared" a function do you mean like this?
declare
function f (p varchar2) return varchar2
is
begin
...
end;
begin
open p_cursor for select * from employee where emp_name = function(ssn);
...
end;
You can't use a function like that in SQL. You would have to create the function in the database so that it can be used outside the PL/SQL program - for example:
create or replace function f (p varchar2) return varchar2
is
begin
...
end;
Or if you are creating a package then you can make the packaged function public like this:
create or replace package pkg is
function f (p varchar2) return varchar2;
...
end;
Then it can be used in other code like this:
open p_cursor for select * from employee where emp_name = pkg.f(ssn);
I am pretty sure you cannot use a group function in a where clause. But this function(ssn) being not a group function cannot also be used here.
You can use it as a sub query, serves the purpose.
open p-cursor for select * from employee where emp_name = (select package_name.function_name(ssn) from dual);

How to call PL/SQL function in side a trigger

I am new to pl/sql. can any one tell me how to call pl/sql function inside a trigger.
I tired it but it gives an error when i try to run it.
DROP TRIGGER INTF_CONTROLLER_TREXE;
CREATE OR REPLACE TRIGGER INTF_CONTROLLER_TREXE
before insert ON INTF_CONTROLLER for each row
begin
BACKOFFICE_UPDATE();
end;
CREATE OR REPLACE FUNCTION BACKOFFICE_UPDATE
RETURN NUMBER IS
tmpVar NUMBER;
BEGIN
tmpVar := 0;
DBMS_OUTPUT.put_line ('HELLO');
RETURN tmpVar;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
WHEN OTHERS THEN
-- Consider logging the error and then re-raise
RAISE;
END BACKOFFICE_UPDATE;
I tried to run it using TOAD. it gives the following error
PLS-00221: 'BACKOFFICE_UPDATE' is not a procedure or is undefined
You need to store the result of your function call in a local variable
For example:
CREATE OR REPLACE TRIGGER INTF_CONTROLLER_TREXE
before insert ON INTF_CONTROLLER for each row
declare
dummy NUMBER;
begin
dummy := BACKOFFICE_UPDATE();
end;

Resources