那需要写一个过程了。
但是你完全可以用可视化工具写程序来进行。
CREATE OR REPLACE PROCEDURE FileExec(
-- Executes the SQL statements in the file identified by
-- p_Directory and p_FileName. Each statement should not contain
-- the trailing semicolon (unless it is a PL/SQL block) and should
-- be separated by p_SeparationChar.
p_Directory IN VARCHAR2,
p_FileName IN VARCHAR2,
p_SeparationChar IN CHAR) AS v_FileLocator BFILE;
v_CLOBLocator CLOB;
v_SQLCursor INTEGER;
v_StartPoint INTEGER := 1;
v_EndPoint INTEGER;
v_SQLStatement VARCHAR2(32000);
v_StatementLength INTEGER;
v_RC INTEGER;
BEGIN
-- Initialize the character locator for writing. Note that we have
-- to select a CLOB from a table FOR UPDATE. This locks the row,
-- and is a requirement for LOADFROMFILE.
SELECT clob_col
INTO v_CLOBLocator
FROM lobdemo
WHERE key = -1
FOR UPDATE; -- Initialize the BFILE locator for reading.
v_FileLocator := BFILENAME(p_Directory, p_FileName);
DBMS_LOB.FILEOPEN(v_FileLocator, DBMS_LOB.FILE_READONLY); -- Set up the cursor.
v_SQLCursor := DBMS_SQL.OPEN_CURSOR; -- Load the entire file into the character LOB.
-- This is necessary so that we have the data in
-- character rather than RAW variables.
DBMS_LOB.LOADFROMFILE(v_CLOBLocator, v_FileLocator,
DBMS_LOB.GETLENGTH(v_FileLocator)); -- Loop over the LOB, searching for each occurrence of
-- the separation character.
LOOP
v_EndPoint := DBMS_LOB.INSTR(v_CLOBLocator, p_SeparationChar,
v_StartPoint, 1);
EXIT WHEN v_EndPoint = 0; -- Extract the contents between the starting and ending points.
-- This is the SQL statement to be executed.
v_StatementLength := v_EndPoint - v_StartPoint;
v_SQLStatement := DBMS_LOB.SUBSTR(v_CLOBLocator,
v_StatementLength, v_StartPoint); -- Echo the statement to the screen, and then execute it
-- using DBMS_SQL.
DBMS_OUTPUT.PUT_LINE(v_SQLStatement);
DBMS_SQL.PARSE(v_SQLCursor, v_SQLStatement, DBMS_SQL.V7);
v_RC := DBMS_SQL.EXECUTE(v_SQLCursor); -- Increment the statement pointer for the next statement.
v_StartPoint := v_EndPoint + 1;
END LOOP; -- Clean up.
DBMS_LOB.FILECLOSE(v_FileLocator);
DBMS_SQL.CLOSE_CURSOR(v_SQLCursor);
EXCEPTION
WHEN OTHERS THEN
-- Close the cursor and file, and reraise.
DBMS_LOB.FILECLOSE(v_FileLocator);
DBMS_SQL.CLOSE_CURSOR(v_SQLCursor);
RAISE;
END FileExec;
/
但是你完全可以用可视化工具写程序来进行。
CREATE OR REPLACE PROCEDURE FileExec(
-- Executes the SQL statements in the file identified by
-- p_Directory and p_FileName. Each statement should not contain
-- the trailing semicolon (unless it is a PL/SQL block) and should
-- be separated by p_SeparationChar.
p_Directory IN VARCHAR2,
p_FileName IN VARCHAR2,
p_SeparationChar IN CHAR) AS v_FileLocator BFILE;
v_CLOBLocator CLOB;
v_SQLCursor INTEGER;
v_StartPoint INTEGER := 1;
v_EndPoint INTEGER;
v_SQLStatement VARCHAR2(32000);
v_StatementLength INTEGER;
v_RC INTEGER;
BEGIN
-- Initialize the character locator for writing. Note that we have
-- to select a CLOB from a table FOR UPDATE. This locks the row,
-- and is a requirement for LOADFROMFILE.
SELECT clob_col
INTO v_CLOBLocator
FROM lobdemo
WHERE key = -1
FOR UPDATE; -- Initialize the BFILE locator for reading.
v_FileLocator := BFILENAME(p_Directory, p_FileName);
DBMS_LOB.FILEOPEN(v_FileLocator, DBMS_LOB.FILE_READONLY); -- Set up the cursor.
v_SQLCursor := DBMS_SQL.OPEN_CURSOR; -- Load the entire file into the character LOB.
-- This is necessary so that we have the data in
-- character rather than RAW variables.
DBMS_LOB.LOADFROMFILE(v_CLOBLocator, v_FileLocator,
DBMS_LOB.GETLENGTH(v_FileLocator)); -- Loop over the LOB, searching for each occurrence of
-- the separation character.
LOOP
v_EndPoint := DBMS_LOB.INSTR(v_CLOBLocator, p_SeparationChar,
v_StartPoint, 1);
EXIT WHEN v_EndPoint = 0; -- Extract the contents between the starting and ending points.
-- This is the SQL statement to be executed.
v_StatementLength := v_EndPoint - v_StartPoint;
v_SQLStatement := DBMS_LOB.SUBSTR(v_CLOBLocator,
v_StatementLength, v_StartPoint); -- Echo the statement to the screen, and then execute it
-- using DBMS_SQL.
DBMS_OUTPUT.PUT_LINE(v_SQLStatement);
DBMS_SQL.PARSE(v_SQLCursor, v_SQLStatement, DBMS_SQL.V7);
v_RC := DBMS_SQL.EXECUTE(v_SQLCursor); -- Increment the statement pointer for the next statement.
v_StartPoint := v_EndPoint + 1;
END LOOP; -- Clean up.
DBMS_LOB.FILECLOSE(v_FileLocator);
DBMS_SQL.CLOSE_CURSOR(v_SQLCursor);
EXCEPTION
WHEN OTHERS THEN
-- Close the cursor and file, and reraise.
DBMS_LOB.FILECLOSE(v_FileLocator);
DBMS_SQL.CLOSE_CURSOR(v_SQLCursor);
RAISE;
END FileExec;
/
SQL>grant create any directory to scott;
SQL>grant create any library to scott;
在scott这个用户下执行下述语句:SQL>create table bfile_tab (bfile_column BFILE);
SQL>create table utl_lob_test (blob_column BLOB);
SQL>create or replace directory utllobdir as 'C:\DDS\EXTPROC';
SQL>set serveroutput on然后执行下面语句就将C:\DDS\EXTPROC目录下的word文件COM.doc存入到utl_lob_test
表中的blob_column字段中了。declare
a_blob BLOB;
a_bfile BFILE := BFILENAME('UTLLOBDIR','COM.doc'); --用来指向外部操作系统文件
begin
insert into bfile_tab values (a_bfile)
returning bfile_column into a_bfile;
insert into utl_lob_test values (empty_blob())
returning blob_column into a_blob;
dbms_lob.fileopen(a_bfile);
dbms_lob.loadfromfile(a_blob, a_bfile, dbms_lob.getlength(a_bfile));
dbms_lob.fileclose(a_bfile);
commit;
end;
/系统报错:ERROR 位于第 1 行:
ORA-22288: 文件或 LOB 操作FILEOPEN失败
系统找不到指定的路径。
ORA-06512: 在"SYS.DBMS_LOB", line 504
ORA-06512: 在line 9
CREATE TABLE MAP
(MAP_NO NEMBUR(2) PRIMARYKEY,
MAP_DATE DATE,
MAP_BLOB BLOB DEFAULT EMPTY_BLOB)想把E:\test.jpg存放到MAP_BLOB,请问应用什么命令,参数是什么create or replace directory utllobdir as 'e:\';
create table bfile_tab (bfile_column BFILE);
create table utl_lob_test (blob_column BLOB);declare
a_blob BLOB;
a_bfile BFILE := BFILENAME('UTLLOBDIR','test.jpg');
begin
insert into bfile_tab values (a_bfile)
returning bfile_column into a_bfile;
insert into utl_lob_test values (empty_blob())
returning blob_column into a_blob;
dbms_lob.fileopen(a_bfile);
dbms_lob.loadfromfile(a_blob, a_bfile, dbms_lob.getlength(a_bfile));
dbms_lob.fileclose(a_bfile);
commit;
end;
/
select dbms_lob.getlength(blob_column) from UTL_LOB_TEST;