Issue With PL/SQL Bind Variables - plsql

Recently, I deployed an Oracle WareHouse Builder (OWB) mapping. In the scenario I'm working right now, this mapping (ETL process) needs to be fired up by a trigger after an Update statement takes place on the fact table (working with WriteBack values).
As the mapping is deployed to the target schema as a package, the trigger must call the main procedure that OWB creates for the package. At first I didn't knew how to accomplish this task, but SQL Developer gave me a hint:
So, I took this code and put it inside my trigger. Like this:
CREATE OR REPLACE TRIGGER RESPALDO_HISTORIAL
AFTER UPDATE ON MONITOR_FT_TAB
FOR EACH ROW
DECLARE
P_STATUS VARCHAR2(200);
P_MAX_NO_OF_ERRORS VARCHAR2(200);
P_COMMIT_FREQUENCY VARCHAR2(200);
P_OPERATING_MODE VARCHAR2(200);
P_BULK_SIZE VARCHAR2(200);
P_AUDIT_LEVEL VARCHAR2(200);
P_PURGE_GROUP VARCHAR2(200);
P_JOB_AUDIT VARCHAR2(200);
BEGIN
P_MAX_NO_OF_ERRORS := NULL;
P_COMMIT_FREQUENCY := NULL;
P_OPERATING_MODE := NULL;
P_BULK_SIZE := NULL;
P_AUDIT_LEVEL := NULL;
P_PURGE_GROUP := NULL;
P_JOB_AUDIT := 'TRUE';
SINIESTROS_MARCADOS_MAP.MAIN(
P_STATUS => P_STATUS,
P_MAX_NO_OF_ERRORS => P_MAX_NO_OF_ERRORS,
P_COMMIT_FREQUENCY => P_COMMIT_FREQUENCY,
P_OPERATING_MODE => P_OPERATING_MODE,
P_BULK_SIZE => P_BULK_SIZE,
P_AUDIT_LEVEL => P_AUDIT_LEVEL,
P_PURGE_GROUP => P_PURGE_GROUP,
P_JOB_AUDIT => P_JOB_AUDIT
);
:P_STATUS := P_STATUS;
END RESPALDO_HISTORIAL;
/
When I tried to compile this trigger, I got this screen:
In this screen I tried clicking "Aplicar" (Apply in spanish) with and without the NULL checkbox, always getting this output:
TRIGGER RESPALDO_HISTORIAL compilado
Errors: check compiler log
Then I ran the SHOW ERRORS command and I got this:
33/3 PLS-00049: bad bind variable 'P_STATUS'
Now I don't quite understand these bind variables. If this is the code generated by SQL Developer to run the package, then why I get this error??
Please help! I need some guidelines in this matter!
Greetings!

A colon preceding a variable indicates that that variable is a bind variable. Bind variables of this type are typically used to pass values in and out of anonymous blocks. They're not allowed in procedures, functions, or triggers. In this case, you need to remove the line :P_STATUS := P_STATUS;.

Related

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

How can I create a new SQLite file and table at runtime using FieldDefs?

I'm using Delphi Seattle to create a brand new table in a brand new SQLite file and using only FieldDefs and non-visual code. I can create a table using the ExecSQL ('CREATE TABLE....' ) syntax but not as shown below (I get 'No such table 'MyTable' which is raised when I execute the CreateDataSet call). I'd like some solution that allows me to work with FieldDefs. This code is modelled on the example here. I notice though, that there is note regarding CreateDataSet that it only applies to TFDMemTable. Is there a runtime way of creating an SQLite table without using ExecSQL?
procedure Test;
const
MyDBFile = 'c:\scratch\hope.db';
var
Connection : TFDConnection;
DriverLink : TFDPhysSQLiteDriverLink;
Table : TFDTable;
begin
DeleteFile( MyDBFile );
DriverLink := TFDPhysSQLiteDriverLink.Create( nil );
Connection := TFDConnection.Create( nil );
try
Connection.Params.Values['DriverID'] := 'SQLite';
Connection.Params.Values['Database'] := MyDBFile;
Connection.Connected := True;
Table := TFDTable.Create( nil );
try
Table.TableName := 'MyTable';
Table.Connection := Connection;
Table.FieldDefs.Add( 'one', ftString, 20 );
Table.FieldDefs.Add( 'two', ftString, 20 );
Table.CreateDataSet;
// I would add records here....
finally
Table.Free;
end;
finally
Connection.Free;
DriverLink.Free;
end;
end;
CreateDataSet is usually a local operation for initializing a client-side dataset into an empty state. If TClientDataSet is anything to go by, afaik it cannot be used create a server-side table.
To create an actual server table, I would expect to have to construct the DDL SQL to create the table and then execute it using ExecSQL on the (client-side) dataset, as you have already tried.
update
The following seems to satisfy your requirement to do everything in code, though using a TFDTable component, which doesn't surface FieldDefs, so I've used code-created TFields instead. Tested in D10 Seattle.
procedure TForm3.CreateDatabaseAndTable;
const
DBName = 'd:\delphi\code\sqlite\atest.sqlite';
var
AField : TField;
begin
if FileExists(DBName) then
DeleteFile(DBName);
AField := TLargeIntField.Create(Self);
AField.Name := 'IDField';
AField.FieldName := 'ID';
AField.DataSet := FDTable1;
AField := TWideStringField.Create(Self);
AField.Size := 80;
AField.Name := 'NameField';
AField.FieldName := 'Name';
AField.DataSet := FDTable1;
FDConnection1.Params.Values['database'] := DBName;
FDConnection1.Connected:= True;
FDTable1.TableName := 'MyTable';
FDTable1.CreateTable(False, [tpTable]);
FDTable1.Open();
FDTable1.InsertRecord([1, 'First']);
FDConnection1.Commit;
FDConnection1.Connected:= False;
end;
I expect that someone a bit more familiar than I am could do similar using a TFDMemTable's FieldDefs if it were correctly connected to a server-side component (FDCommand?) via an FDTableAdaptor.
Fwiw, I've used a LargeInt ID field and WideString Name field because trying to use Sqlite with D7 a while back, I had no end of trouble trying to use Integer and string fields.
Btw, you if you know the structure you require in advance of deployment, you might find that you get more predictable/robust results if you simply copy an empty database + table into place, rather than try and create the table in situ. Ymmv, of course.
I would NEVER dream of creating database tables using fielddefs because you wind up having tables without a proper primary key, indexes and referential integrity. The resulting tables are totally "dumbed down".
Whenever you have a "where" clause in a query the database would do a full table scan to find the records matching the query. So your database slows down (and CPU use increases) with size. That's just bad design.
Regards,
Arthur
You can use the app SQLite Expert Professional, create SQLite database.
And using FDConnection connect to the database. And use it.
Method to database SQLite, the same way that MartynA have said.
Begin
FDConnection1.Connected:=false;
FDConnection1.Params.Clear;
FDConnection1.Params.Database:='D:\SQLiteDatabase.db';
FDConnection1.ConnectionDefName:='SQLite_Demo';
FDConnection1.DriverName:='SQLite';
FDConnection1.Connected:=true;
FDTable1.Open;
End;

PLS-00487 Error-Invalid reference to Variable 'CHAR'

I'm designing a function that is part of a larger package. The function is intended to take a District Code and return a collection of unique IDs for 10-15 stores that are assigned to that district. The function is intended to return a collection that can be queried like a table, i.e., using the TABLE function in a SQL statement.
I've created the following Types:
Schema Level type:
create or replace TYPE HDT_CORE_ORGIDS AS TABLE OF CHAR(20);
and a Type inside the Package
TYPE CORE_ORGIDS IS TABLE OF CHAR(20) INDEX BY BINARY_INTEGER;
Here's the function code:
FUNCTION FindDistrictOrgs(
ParamOrgCode VARCHAR2
)
RETURN HDT_CORE_ORGIDS
AS
ReturnOrgs HDT_CORE_ORGIDS := HDT_CORE_ORGIDS();
FDOTemp HDT_CORE_MAIN.CORE_ORGIDS;
i BINARY_INTEGER := 0;
CURSOR FDOCurr IS
SELECT org.id AS OrgID
FROM tp2.tpt_company org
WHERE LEVEL = 2
START WITH org.name = ParamOrgCode
CONNECT BY PRIOR org.id = org.parent_id;
BEGIN
OPEN FDOCurr;
LOOP
i := i +1;
FETCH FDOCurr INTO FDOTemp(i);
EXIT WHEN FDOCurr%NOTFOUND;
END LOOP;
IF FDOTemp.EXISTS(FDOTemp.FIRST) THEN
ReturnOrgs.EXTEND(FDOTemp.LAST);
FOR x IN FDOTemp.FIRST .. FDOTemp.LAST LOOP
ReturnOrgs(x) := FDOTemp(x).OrgID;
END LOOP;
END IF;
CLOSE FDOCurr;
RETURN ReturnOrgs;
END FindDistrictOrgs ;
I'm getting the PLS-00487:Invalid Reference to variable 'CHAR' at the line:
ReturnOrgs(x) := FDOTemp(x).OrgID;
I've double-checked at the value returned by the SQL (the org.id AS OrgID) is of the CHAR(20 BYTE) datatype.
So...what's causing the error?
Any help is appreciated! :)
OrgID is the alias you gave the column in your cursor, it has no meaning to the collection. Since both collections are of simple types you should just be doing:
ReturnOrgs(x) := FDOTemp(x);
The syntax you're using is implying FDOTemp is a collection of objects and you're trying to reference the OrgID attribute of an object; but since CHAR isn't an object type, this errors. The error message even makes some sense when viewed like that, though it's not terribly helpful if you don't already know what's wrong... and not entirely helpful when you do.
Incidentally, you could use a bulk collect to populate the collection without the cursor or loops, or the extra collection:
SELECT org.id
BULK COLLECT INTO ReturnOrgs
FROM tp2.tpt_company org
WHERE LEVEL = 2
START WITH org.name = ParamOrgCode
CONNECT BY PRIOR org.id = org.parent_id;
RETURN ReturnOrgs;

Debugging a function with table type as input parameter

Can anybody let me know how can i debug a functions with table type as input parameter and this function returns a table type pipelined.
Please see below details.When i try to test the function it creates below anonymous block but when i click on debug button it gives error:
Anonymous block:
declare
-- Non-scalar parameters require additional processing
result t_bmk_q;
pit_srch_str t_parm;
begin
-- Call the function
result := f_bmk_srch(pit_srch_str => pit_srch_str,
piv_op => 'ALL');
end;
---f_bmk_q function returns table type t_bmk_q pipelined
defintions:
==============
t_bmk_q --->table type
t_bmk_q is TABLE OF r_bmk_q -->object of some attributes.
pit_srch_str ---> is parameter of type t_parm which is table type of r_parm
--plz see def of r_parm:
CREATE OR REPLACE TYPE r_parm AS OBJECT
(
p_abc varchar2(200)
,p_new_val varchar2(2000)
,CONSTRUCTOR FUNCTION r_parm
(
p_abc varchar2
,p_new_val varchar2
) RETURN SELF AS RESULT
);
Example:I have below sample values to test and debug:
r_parm('TAB1.VALUE','123321123')
Thanks
Rajesh
It seems you are using a PL/SQL Developer Test Window to run the tests. I recognise the comment Non-scalar parameters require additional processing.
PL/SQL Developer's Test Window doesn't handle pipelined functions too well.
You are best off removing the result variable and wrapping the function call in open :cursor for select * from table(...). Add a variable named cursor of type Cursor to the variables list below the window.
To populate the input table, you can simply 'call' the table type, passing each row in the table as a separate argument. For example,
t_parm(r_parm(...), r_parm(...), r_parm(...))
You can add as many rows to the table as you like this way.
Putting both of these changes together, we have something like the following:
declare
pit_srch_str t_parm;
begin
-- Create input table.
pit_srch_str := t_parm(
r_parm('TAB1.VALUE','123321123'),
r_parm('TAB2.VALUE','456597646')
);
-- Call the function
open :cursor for select * from table(f_bmk_srch(pit_srch_str => pit_srch_str,
piv_op => 'ALL'));
end;
/
Once you've run the function, you can get the results from the cursor variable (use the ... button at the far right).

Unable to return query output in Apex PL SQL expression

I am trying to write a following PL/SQL function body for a dynamic action
The purpose of dynamic action is to set value for text area based on input parameters. Way I am trying to do it, is that setting the value into variable for different options
declare
P_NOTE varchar(100); -- derive value
P_WEBSERVER varchar(100); -- derive name
begin
-- for getting the P_NOTE value
select distinct note into P_NOTE from port_mapping where PLATFORM = :P3_PLATFORM and VERSION = :P3_VERSION;
-- for getting web server value
select CONCAT(P_NOTE,CONCAT('https-',:P3_CLIENT)) into P_WEBSERVER from dual order by 1;
if (:P3_PLATFORM = 'Apache') then
return P_WEBSERVER;
end if;
end;
However I am getting error
ORA-06550: line 15, column 5:
PLS-00372: In a procedure, RETURN statement cannot contain an expression
ORA-06550: line 15, column 5:
PL/SQL: Statement ignored
declare
P_NOTE varchar(100);
P_WEBSERVER varchar(100);
I am not sure what I am missing.
(Since you did not post any apex version this explanation deals with version 4.2)
If this -is- a dynamic action and the code you posted is in a true action of type 'Execute PL/SQL Code' then you can not use RETURN. The plsql block is not a function body (close, Mr Kemp!).
If you want to return values from the session state to page items then you need to use the "Page Items to Return" item of the true action.
This will put the session state of the defined page items into the value of the item on the page. This means that you can not use any variable to just put stuff in to be able to return it to the page, but you need to use an actual page item (after all, these are bind variables).
To clarify further, you would not write :
return P_WEBSERVER;
But you'd have to use a page item, say P3_WEBSERVER, and you'll need to create one if it doesn't exist of course:
:P3_WEBSERVER := p_webserver;
Of course you'd need to make sure that the correct value will be in there as you can not shortcircuit as you did in your code sample (p_webserver will usually hold a value even if the platform is not 'Apache') eg:
if (:P3_PLATFORM = 'Apache') then
:P3_WEBSERVER := P_WEBSERVER;
else
:P3_WEBSERVER := NULL;
end if;
Just read error message:
line 15, column 5
So, trouble caused by this line:
return P_WEBSERVER;
return not allowed in PL/SQL blocks, use output parameter to return a value.
Read Tom's answer to find out how to do that.

Resources