Friday, February 15, 2013

Upload file to BLOB type column/field

CREATE TABLE t (id NUMBER(10),doc BLOB );


CREATE SEQUENCE t_seq;


CREATE OR REPLACE DIRECTORY documents AS 'd:\app';


Create procedure:
CREATE OR REPLACE PROCEDURE upload_t (file_name_v IN varchar) AS
v_bfile BFILE;
v_blob BLOB;
BEGIN
INSERT INTO t (id, doc) vALUES (t_seq.NEXTVAL, empty_blob()) RETURN doc INTO v_blob;
v_bfile := BFILENAME('DOCUMENTS', file_name_v);
Dbms_Lob.Fileopen(v_bfile, Dbms_Lob.File_Readonly);
Dbms_Lob.Loadfromfile(v_blob, v_bfile, Dbms_Lob.Getlength(v_bfile));
Dbms_Lob.Fileclose(v_bfile);

COMMIT;
end;
/


SQL> EXEC upload_t('dp.log');

--PL/SQL procedure successfully completed.




If You want to update the column data, go with below procedure:
CREATE OR REPLACE PROCEDURE update_t (file_name_v IN varchar) AS
  v_bfile  BFILE;
  v_blob   BLOB;
BEGIN
  SELECT DOC INTO v_blob FROM t WHERE  id = 3 FOR UPDATE;

  v_bfile := BFILENAME('DOCUMENTS',file_name_v );
  Dbms_Lob.Fileopen(v_bfile, Dbms_Lob.File_Readonly);
  Dbms_Lob.Loadfromfile(v_blob, v_bfile, Dbms_Lob.Getlength(v_bfile));
  Dbms_Lob.Fileclose(v_bfile);
END;
/

No comments:

Post a Comment

web stats