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');
Related
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
I have just created a job using SQL Developer build-in wizard and i would like to change the parameters of the created job from my application.
The job is launching a stored procedure every day at 7 o'clock.
In the application I have to field:
Disable: true/false
Hour: 10:00
And this is my job and fields I would like to update depending on what was selected in the app:
I see two ways of doing it:
Make the fields dependent on the values in the table
Make a pl/sql block that updates the values
Of course I don't know how to do it (in the first option the select statement doesn't work and in the second I don't know how to acces the jobs fields)
Please help
Begin
dbms_scheduler.disable( 'ATOS."job_email_notifications"' );
DBMS_SCHEDULER.SET_ATTRIBUTE ( name => 'ATOS."job_email_notifications"', attribute => 'repeat_interval', value => 'freq=daily;byday=FRI,SAT;byhour=20;byminute=0; bysecond=0');--change value--as per need
dbms_scheduler.enable( 'ATOS."job_email_notifications"' );
End;
/
Use below procedure for dynamic updates based on values
Create or replace procedure change_attributes(a_job_name varchar2, a_param varchar2, a_new_val varchar2)
As
Begin
dbms_scheduler.disable( a_job_name);
DBMS_SCHEDULER.SET_ATTRIBUTE ( name => a_job_name,attribute => a_param, value => a_new_val);
dbms_scheduler.enable( a_job_name);
End;
/
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;
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;.
I am teaching myself to use SQLite and FireDAC together in Delphi. I am not very experienced with the latest incarnations of databases and tools so after writing a very simple application to display a single table from an SQLite file, I decided that I would put together a simple viewer 'frame' that would help me learn and maybe give me (eventually) a debugging tool to put in my Application for engineering use.
So, I've used a simple TTreeView and I wish to populate it with a hierarchy of 'databases' (catalogues?), 'tables', 'field names' and 'field types'. So far it has been remarkably easy to list the catalogues, tables and fields (using TFDConnection.Getxxxxx) but I cant see how to go deeper to get field definitions. Is this possible to do from a TFDConnection? Or do I need to open a temporary query?
My existing code is shown below and my 'field types' would be a further nested loop when shown as '// xxxxxxxxxxxxxxxxxxx'
procedure TForm1.Button1Click(Sender: TObject);
procedure DatabaseToTreeView( AConnection : TFDConnection; ATreeView : TTreeView );
procedure ProcessConnection;
procedure ProcessCatalogueName( const ACatalogueName : string; ARoot : TTreeNode );
procedure ProcessTableName( const ATableName : string; ARoot : TTreeNode );
var
List : TStrings;
{Node : TTreeNode;}
I : integer;
begin
List := TStringList.Create;
try
AConnection.GetFieldNames( ACatalogueName, '', ATableName, '', List );
for I := 0 to List.Count-1 do
begin
{Node := }ATreeView.Items.AddChild( ARoot, List[I] );
// xxxxxxxxxxxxxxxxxxx
end;
finally
List.Free;
end;
end;
var
List : TStrings;
Node : TTreeNode;
I : integer;
begin
List := TStringList.Create;
try
AConnection.GetTableNames( ACatalogueName, '', '', List );
for I := 0 to List.Count-1 do
begin
Node := ATreeView.Items.AddChild( ARoot, List[I] );
ProcessTableName( List[I], Node );
end;
finally
List.Free;
end;
end;
var
List : TStrings;
Node : TTreeNode;
I : integer;
begin
List := TStringList.Create;
try
AConnection.GetCatalogNames( '', List );
if List.Count = 0 then
ProcessCatalogueName( '', nil )
else
for I := 0 to List.Count-1 do
begin
Node := ATreeView.Items.AddChild( nil, List[I] );
ProcessCatalogueName( List[I], Node );
end;
finally
List.Free;
end;
end;
begin
ATreeView.Items.Clear;
ATreeView.Items.BeginUpdate;
try
ProcessConnection;
finally
ATreeView.Items.EndUpdate;
end;
end;
begin
FDConnection1.Open;
FDQuery1.Active := true;
DatabaseToTreeView( FDConnection1, TreeView1 );
end;
Many thanks, Brian.
One solution is to instantiate a TFDTable, connect it to AConnection and call FieldDefs.Update. This won't fetch any data.
Use the TFDMetaInfoQuery component. It is unified for getting metadata information, so it's usable with any kind of supported DBMS. It populates metadata resultsets by the requested MetaInfoKind and given DBMS object description.
Instead of using a temporary query with a false condition to get table schema without any data (such as "select * from tablename where 1=0" - I assume that is what you meant), depending on your database, you could also use a query to get table information. Like;
For MySQL:
show columns from tablename;
For SQLite:
PRAGMA table_info(tablename)
For MS SQL Server:
select column_name, data_type, character_maximum_length from INFORMATION_SCHEMA.COLUMNS where table_name = 'tablename';
I believe PostgreSQL also has such a function, however I dont have a pgsql installation handy at this time.