📄 lob_schema.sql
字号:
v_return INTEGER;
BEGIN
-- use CREATETEMPORARY() to create a temporary CLOB named v_clob
DBMS_LOB.CREATETEMPORARY(v_clob, TRUE);
-- use WRITE() to write the contents of v_char_buffer to v_clob
v_amount := LENGTH(v_char_buffer);
DBMS_LOB.WRITE(v_clob, v_amount, v_offset, v_char_buffer);
-- use READ() to read the contents of v_clob into v_char_buffer
DBMS_LOB.READ(v_clob, v_amount, v_offset, v_char_buffer);
DBMS_OUTPUT.PUT_LINE('v_char_buffer = ' || v_char_buffer);
-- use INSTR() to search v_clob for the second occurrence of is,
-- and INSTR() returns 29
DBMS_OUTPUT.PUT_LINE('Searching for second ''is''');
v_pattern := 'is';
v_occurrence := 2;
v_return := DBMS_LOB.INSTR(v_clob, v_pattern, v_offset, v_occurrence);
DBMS_OUTPUT.PUT_LINE('v_return = ' || v_return);
-- use INSTR() to search v_clob for the first occurrence of Moon,
-- and INSTR() returns 0 because Moon doesn抰 appear in v_clob
DBMS_OUTPUT.PUT_LINE('Searching for ''Moon''');
v_pattern := 'Moon';
v_occurrence := 1;
v_return := DBMS_LOB.INSTR(v_clob, v_pattern, v_offset, v_occurrence);
DBMS_OUTPUT.PUT_LINE('v_return = ' || v_return);
-- use FREETEMPORARY() to free v_clob
DBMS_LOB.FREETEMPORARY(v_clob);
END instr_example;
/
CREATE PROCEDURE copy_file_data_to_clob(
p_clob_id INTEGER,
p_directory VARCHAR2,
p_file_name VARCHAR2
) AS
v_file UTL_FILE.FILE_TYPE;
v_chars_read INTEGER;
v_dest_clob CLOB;
v_amount INTEGER := 32767;
v_char_buffer VARCHAR2(32767);
BEGIN
-- insert an empty CLOB
INSERT INTO clob_content(
id, clob_column
) VALUES (
p_clob_id, EMPTY_CLOB()
);
-- get the LOB locator of the CLOB
SELECT clob_column
INTO v_dest_clob
FROM clob_content
WHERE id = p_clob_id
FOR UPDATE;
-- open the file for reading of text (up to v_amount characters per line)
v_file := UTL_FILE.FOPEN(p_directory, p_file_name, 'r', v_amount);
-- copy the data from the file into v_dest_clob one line at a time
LOOP
BEGIN
-- read a line from the file into v_char_buffer;
-- GET_LINE() does not copy the newline character into
-- v_char_buffer
UTL_FILE.GET_LINE(v_file, v_char_buffer);
v_chars_read := LENGTH(v_char_buffer);
-- append the line to v_dest_clob
DBMS_LOB.WRITEAPPEND(v_dest_clob, v_chars_read, v_char_buffer);
-- append a newline to v_dest_clob because v_char_buffer;
-- the ASCII value for newline is 10, so CHR(10) returns newline
DBMS_LOB.WRITEAPPEND(v_dest_clob, 1, CHR(10));
EXCEPTION
-- when there is no more data in the file then exit
WHEN NO_DATA_FOUND THEN
EXIT;
END;
END LOOP;
-- close the file
UTL_FILE.FCLOSE(v_file);
DBMS_OUTPUT.PUT_LINE('Copy successfully completed.');
END copy_file_data_to_clob;
/
CREATE PROCEDURE copy_file_data_to_blob(
p_blob_id INTEGER,
p_directory VARCHAR2,
p_file_name VARCHAR2
) AS
v_file UTL_FILE.FILE_TYPE;
v_bytes_read INTEGER;
v_dest_blob BLOB;
v_amount INTEGER := 32767;
v_binary_buffer RAW(32767);
BEGIN
-- insert an empty BLOB
INSERT INTO blob_content(
id, blob_column
) VALUES (
p_blob_id, EMPTY_BLOB()
);
-- get the LOB locator of the BLOB
SELECT blob_column
INTO v_dest_blob
FROM blob_content
WHERE id = p_blob_id
FOR UPDATE;
-- open the file for reading of bytes (up to v_amount bytes at a time)
v_file := UTL_FILE.FOPEN(p_directory, p_file_name, 'rb', v_amount);
-- copy the data from the file into v_dest_blob
LOOP
BEGIN
-- read binary data from the file into v_binary_buffer
UTL_FILE.GET_RAW(v_file, v_binary_buffer, v_amount);
v_bytes_read := LENGTH(v_binary_buffer);
-- append v_binary_buffer to v_dest_blob
DBMS_LOB.WRITEAPPEND(v_dest_blob, v_bytes_read/2,
v_binary_buffer);
EXCEPTION
-- when there is no more data in the file then exit
WHEN NO_DATA_FOUND THEN
EXIT;
END;
END LOOP;
-- close the file
UTL_FILE.FCLOSE(v_file);
DBMS_OUTPUT.PUT_LINE('Copy successfully completed.');
END copy_file_data_to_blob;
/
CREATE PROCEDURE copy_clob_data_to_file(
p_clob_id INTEGER,
p_directory VARCHAR2,
p_file_name VARCHAR2
) AS
v_src_clob CLOB;
v_file UTL_FILE.FILE_TYPE;
v_offset INTEGER := 1;
v_amount INTEGER := 32767;
v_char_buffer VARCHAR2(32767);
BEGIN
-- get the LOB locator of the CLOB
SELECT clob_column
INTO v_src_clob
FROM clob_content
WHERE id = p_clob_id;
-- open the file for writing of text (up to v_amount characters at a time)
v_file := UTL_FILE.FOPEN(p_directory, p_file_name, 'w', v_amount);
-- copy the data from v_src_clob to the file
LOOP
BEGIN
-- read characters from v_src_clob into v_char_buffer
DBMS_LOB.READ(v_src_clob, v_amount, v_offset, v_char_buffer);
-- copy the characters from v_char_buffer to the file
UTL_FILE.PUT(v_file, v_char_buffer);
-- add v_amount to v_offset
v_offset := v_offset + v_amount;
EXCEPTION
-- when there is no more data in the file then exit
WHEN NO_DATA_FOUND THEN
EXIT;
END;
END LOOP;
-- flush any remaining data to the file
UTL_FILE.FFLUSH(v_file);
-- close the file
UTL_FILE.FCLOSE(v_file);
DBMS_OUTPUT.PUT_LINE('Copy successfully completed.');
END copy_clob_data_to_file;
/
CREATE PROCEDURE copy_blob_data_to_file(
p_blob_id INTEGER,
p_directory VARCHAR2,
p_file_name VARCHAR2
) AS
v_src_blob BLOB;
v_file UTL_FILE.FILE_TYPE;
v_offset INTEGER := 1;
v_amount INTEGER := 32767;
v_binary_buffer RAW(32767);
BEGIN
-- get the LOB locator of the BLOB
SELECT blob_column
INTO v_src_blob
FROM blob_content
WHERE id = p_blob_id;
-- open the file for writing of bytes (up to v_amount bytes at a time)
v_file := UTL_FILE.FOPEN(p_directory, p_file_name, 'wb', v_amount);
-- copy the data from v_src_blob to the file
LOOP
BEGIN
-- read characters from v_src_blob into v_binary_buffer
DBMS_LOB.READ(v_src_blob, v_amount, v_offset, v_binary_buffer);
-- copy the binary data from v_binary_buffer to the file
UTL_FILE.PUT_RAW(v_file, v_binary_buffer);
-- add v_amount to v_offset
v_offset := v_offset + v_amount;
EXCEPTION
-- when there is no more data in the file then exit
WHEN NO_DATA_FOUND THEN
EXIT;
END;
END LOOP;
-- flush any remaining data to the file
UTL_FILE.FFLUSH(v_file);
-- close the file
UTL_FILE.FCLOSE(v_file);
DBMS_OUTPUT.PUT_LINE('Copy successfully completed.');
END copy_blob_data_to_file;
/
CREATE PROCEDURE copy_bfile_data_to_clob(
p_bfile_id INTEGER,
p_clob_id INTEGER
) AS
v_src_bfile BFILE;
v_directory VARCHAR2(200);
v_filename VARCHAR2(200);
v_length INTEGER;
v_dest_clob CLOB;
v_amount INTEGER := DBMS_LOB.LOBMAXSIZE;
v_dest_offset INTEGER := 1;
v_src_offset INTEGER := 1;
v_src_csid INTEGER := DBMS_LOB.DEFAULT_CSID;
v_lang_context INTEGER := DBMS_LOB.DEFAULT_LANG_CTX;
v_warning INTEGER;
BEGIN
-- get the locator of the BFILE
SELECT bfile_column
INTO v_src_bfile
FROM bfile_content
WHERE id = p_bfile_id;
-- use FILEEXISTS() to check if the file exists
-- (FILEEXISTS() returns 1 if the file exists)
IF (DBMS_LOB.FILEEXISTS(v_src_bfile) = 1) THEN
-- use OPEN() to open the file
DBMS_LOB.OPEN(v_src_bfile);
-- use FILEGETNAME() to get the name of the file and the directory
DBMS_LOB.FILEGETNAME(v_src_bfile, v_directory, v_filename);
DBMS_OUTPUT.PUT_LINE('Directory = ' || v_directory);
DBMS_OUTPUT.PUT_LINE('Filename = ' || v_filename);
-- insert an empty CLOB
INSERT INTO clob_content(
id, clob_column
) VALUES (
p_clob_id, EMPTY_CLOB()
);
-- get the LOB locator of the CLOB (for update)
SELECT clob_column
INTO v_dest_clob
FROM clob_content
WHERE id = p_clob_id
FOR UPDATE;
-- use LOADCLOBFROMFILE() to get up to v_amount characters
-- from v_src_bfile and store them in v_dest_clob, starting
-- at offset 1 in v_src_bfile and v_dest_clob
DBMS_LOB.LOADCLOBFROMFILE(
v_dest_clob, v_src_bfile,
v_amount, v_dest_offset, v_src_offset,
v_src_csid, v_lang_context, v_warning
);
-- check v_warning for an inconvertible character
IF (v_warning = DBMS_LOB.WARN_INCONVERTIBLE_CHAR) THEN
DBMS_OUTPUT.PUT_LINE('Warning! Inconvertible character.');
END IF;
-- use CLOSE() to close v_src_bfile
DBMS_LOB.CLOSE(v_src_bfile);
DBMS_OUTPUT.PUT_LINE('Copy successfully completed.');
ELSE
DBMS_OUTPUT.PUT_LINE('File does not exist');
END IF;
END copy_bfile_data_to_clob;
/
CREATE PROCEDURE copy_bfile_data_to_blob(
p_bfile_id INTEGER,
p_blob_id INTEGER
) AS
v_src_bfile BFILE;
v_directory VARCHAR2(200);
v_filename VARCHAR2(200);
v_length INTEGER;
v_dest_blob BLOB;
v_amount INTEGER := DBMS_LOB.LOBMAXSIZE;
v_dest_offset INTEGER := 1;
v_src_offset INTEGER := 1;
BEGIN
-- get the locator of the BFILE
SELECT bfile_column
INTO v_src_bfile
FROM bfile_content
WHERE id = p_bfile_id;
-- use FILEEXISTS() to check if the file exists
-- (FILEEXISTS() returns 1 if the file exists)
IF (DBMS_LOB.FILEEXISTS(v_src_bfile) = 1) THEN
-- use OPEN() to open the file
DBMS_LOB.OPEN(v_src_bfile);
-- use FILEGETNAME() to get the name of the file and
-- the directory
DBMS_LOB.FILEGETNAME(v_src_bfile, v_directory, v_filename);
DBMS_OUTPUT.PUT_LINE('Directory = ' || v_directory);
DBMS_OUTPUT.PUT_LINE('Filename = ' || v_filename);
-- insert an empty BLOB
INSERT INTO blob_content(
id, blob_column
) VALUES (
p_blob_id, EMPTY_BLOB()
);
-- get the LOB locator of the BLOB (for update)
SELECT blob_column
INTO v_dest_blob
FROM blob_content
WHERE id = p_blob_id
FOR UPDATE;
-- use LOADBLOBFROMFILE() to get up to v_amount bytes
-- from v_src_bfile and store them in v_dest_blob, starting
-- at offset 1 in v_src_bfile and v_dest_blob
DBMS_LOB.LOADBLOBFROMFILE(
v_dest_blob, v_src_bfile,
v_amount, v_dest_offset, v_src_offset
);
-- use CLOSE() to close v_src_bfile
DBMS_LOB.CLOSE(v_src_bfile);
DBMS_OUTPUT.PUT_LINE('Copy successfully completed.');
ELSE
DBMS_OUTPUT.PUT_LINE('File does not exist');
END IF;
END copy_bfile_data_to_blob;
/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -