在服务器中的plsql过程中动态创建文件夹

时间:2016-09-15 16:14:03

标签: plsql oracle-sqldeveloper plsqldeveloper

我写了一个pl / sql程序:

PROCEDURE xx_WriteBLOBToFILE (myfilename IN VARCHAR2,L_PERSON_ID IN NUMBER) IS 

  v_blob         BLOB;
  blob_length    INTEGER;
    out_file utl_file.file_type;
  v_buffer       RAW(32767);
  chunk_size     BINARY_INTEGER := 32767;
  Blob_Position  Integer := 1;
   G_Zipped_Blob Blob;
B_Dl_File1 Blob;
BEGIN

  -- Retrieve the BLOB for reading
  Select Image Into V_Blob From Per_Images 
  Where Parent_Id =L_PERSON_ID;

  -- Retrieve the SIZE of the BLOB
  blob_length:=DBMS_LOB.GETLENGTH(v_blob);

  -- Open a handle to the location where you are going to write the BLOB to file
  -- NOTE: The 'wb' parameter means "write in byte mode" and is only availabe
  --       in the UTL_FILE package with Oracle 10g or later
  Out_File := Utl_File.Fopen ('**INT_DIR_IMG_BLOB**', Myfilename, 'wb', Chunk_Size);

  -- Write the BLOB to file in chunks 
  WHILE blob_position <= blob_length LOOP
    IF blob_position + chunk_size - 1 > blob_length THEN
      chunk_size := blob_length - blob_position + 1;
    End If;

    DBMS_LOB.READ(v_blob, chunk_size, blob_position, v_buffer);
    UTL_FILE.PUT_RAW(out_file, v_buffer, TRUE);
    blob_position := blob_position + chunk_size;
  END LOOP;

  -- Close the file handle
  Utl_File.Fclose (Out_File);
End;

我想在一个位置的/ Blobfile中动态创建文件夹,然后动态创建类似INT_DIR_IMG_BLOB的目录。

如何在plsql

中的服务器中创建文件夹

2 个答案:

答案 0 :(得分:1)

如果操作系统目录尚不存在,则可能无法直接在PL / SQL中创建它。您可以创建一个Java存储过程(如果您的数据库在Windows上运行,则为.Net存储过程),该过程创建操作系统目录并从PL / SQL调用该目录。您还可以创建一个dbms_scheduler作业,该作业调用操作系统来创建目录并从PL / SQL调用该目录。

存在操作系统目录后,可以使用动态SQL创建Oracle目录对象。您可能还需要动态地将该目录对象的权限授予用户。

execute immediate 'create directory ' || directory_name_you_want ||
                  ' as ''' || directory_path || '''';
execute immediate 'grant read on ' || directory_name_you_want ||
                  ' to some_role';

然后,您可以在代码中使用新创建的目录。

虽然你可以这样做,但我通常会建议你不要走这条路。在运行时动态创建对象通常是一个糟糕的想法,它会使您的代码更复杂,更难以支持。并且它以新的和有趣的方式创造了许多出错的机会。

答案 1 :(得分:0)

您可以使用DBMS_SCHEDULER在os中创建文件夹。 1.创建凭据。 2.编写用于外部作业执行的调度程序。

BEGIN
   DBMS_CREDENTIAL.CREATE_CREDENTIAL(
      credential_name  =>  'credintial',   ----- credintial name give by u
      username         =>  'dons',          ----- os username
      password         =>  'password');    ----- os password
END;
/


CREATE OR REPLACE PROCEDURE PROC_MAKEFOLDER AS 
BEGIN
DBMS_SCHEDULER.CREATE_JOB 
(JOB_NAME=>'folder_maker',       --- job name 
JOB_ACTION=>'mkdir /home/anil/new_folder',    --- executable file with path 
JOB_TYPE=>'executable',        -----   job type
NUMBER_OF_ARGUMENTS=>0,  --  parameters in numbers 
ENABLED=>false,                   
AUTO_DROP =>true,
CREDENTIAL_NAME=>'credintial',   -- give credentials name which you have created before "credintial"
COMMENTS=> 'folder or os directory creation');
DBMS_SCHEDULER.RUN_JOB('folder_maker');
END ;

EXEC PROC_MAKEFOLDER;