<=====插入图片========>
create directory img as 'c:\img';
保证在服务器端即192.168.0.250的C盘下有名为img的文件夹并在此目录下有"1.jpg"这张图片并保证当前用户有对此目录的读取权限(如果读取文件用户不是创建目录的用户那么要用创建目录的用户执行grant read on directory img to XXX)
create or replace procedure insert_image(img_dir varchar2,img_name varchar2)
is
img_blob blob;
img_bfile bfile;
begin
/*将通过empty_blob()函数将类型为blob的列初始化为空以便以后填充*/
insert into fw.my_image values(empty_blob())
return fw.my_image.img into img_blob;
img_bfile := bfilename(img_dir,img_name); --获得定位器指向的目录和文件
if (dbms_lob.fileexists(img_bfile)!=0) then --如果文件定位器指向的文件存在
dbms_lob.fileopen(img_bfile,dbms_lob.file_readonly); --打开目标文件
/*将文件字节流数据加载到指定的LOB类型变量中*/
dbms_lob.loadfromfile(img_blob,img_bfile,dbms_lob.getlength(img_bfile));
dbms_lob.fileclose(img_bfile);--关闭文件
commit;
dbms_output.put_line('已经从'||img_dir||'目录中读取了图片'||img_name||'向表中插入');
else--如果文件定位器指向的文件不存在
dbms_output.put_line('文件没找到');
end if;
exception when others then
dbms_output.put_line(sqlerrm);
end;
调用过程
declare
begin
fw.insert_image('IMG','1.jpg');
end;
<====将查询结果转化为XML格式并打印======>
create or replace procedure query_to_xml
is
clob_var clob;
xml_content varchar2(32767);
line_content varchar2(4000);
line_id int; --行号
begin
line_id := 0;
clob_var := dbms_xmlquery.getXML('select * from fw.math'); --将查询SQL返回的结果集合转化为XML格式并交给一个CLOB变量保存
xml_content := dbms_lob.substr(clob_var,32767); --截取此CLOB变量保存的全部数据并交给一个PL/SQL里的varchar2类型变量保存(最大长度为32767)
dbms_output.put_line('以下是math表的记录转化而成的XML文档内容');
while xml_content is not null --循环
loop
line_id := line_id+1; --行号++
line_content := substr(xml_content,1,instr(xml_content,'>')); --提取第1行
dbms_output.put_line(line_id||':'||line_content); --打印行号和行内容
xml_content := substr(xml_content,instr(xml_content,chr(10))+1,length(xml_content)); --把已经读取的行从整个XML数据里剔除掉,那么剩下的XML数据的第1行就是整个XML数据的第2行,依此类推,进行遍历(chr(10)返回一个换行符)
end loop;
/*异常处理*/
exception when others then
dbms_output.put_line(sqlerrm);
end;
length(xml_content)这个参数也可以不写 原因如下:
select substr('abcde',2,3) from dual 结果是bcd
select substr('abcde',2) from dual 结果是bcde 不写第3个参数即不告诉系统你要提取几个字符时默认从第2个字符开始将后面的字符都提取出来
select instr('worldwide','d') from dual 结果是5 即返回第2个参数在字符串里第1次出现的位置