1. 授于test用戶文件讀寫和執(zhí)行命令的權(quán)限
在巧家等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站設(shè)計(jì)制作、網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需規(guī)劃網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,網(wǎng)絡(luò)營銷推廣,外貿(mào)網(wǎng)站制作,巧家網(wǎng)站建設(shè)費(fèi)用合理。
SQLexec dbms_java.grant_permission('TEST','SYS:java.io.FilePermission','ALL FILES','read,write,execute,delete');
SQLexec dbms_java.grant_permission('TEST','java.lang.RuntimePermission','*','writeFileDescriptor' );
2. 建立java source
CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "bb" as import java.io.*;
import java.lang.*;
import java.util.*;
import java.sql.*;
import oracle.sql.*;
public class bb
{
public static void invoke_exe() throws IOException
{
Process p=Runtime.getRuntime().exec("d:/exp_74.bat");--注意/符號
try
{
p.waitFor();
}catch(InterruptedException ie){System.out.println(ie);}
}
}
/
3. 建立調(diào)用java source的存儲過程CALL_BB
create or replace procedure CALL_BB
as
language java
name 'bb.invoke_exe()';
/
4. 執(zhí)行CALL_BB即可;
create?table?inputfile?(
filename?varchar2(30),
xmlcontent??xmltype,
sqlcontent??varchar2(3000)
);
create?or?replace?directory?TESTFILE?as?'D:\work\oralce\';
create?or?replace?procedure?alter_table?(pi_file_name?in?varchar2)
is
filehandle?utl_file.file_type;?
filebuffer?varchar2(32767);?
l_clob?????clob;
doc????????DBMS_XMLDOM.DOMDocument;
CDATA??????VARCHAR2(3000);
begin?
filehandle?:=?utl_file.fopen('TESTFILE',pi_file_name,'R');?
IF?utl_file.is_open(filehandle)?THEN?
dbms_output.put_line('file?is?open!');?
END?IF;
loop?
begin?
utl_file.get_line(filehandle,filebuffer);?
dbms_output.put_line(filebuffer);?
l_clob?:=?l_clob||filebuffer;
EXCEPTION?
WHEN?no_data_found?THEN?
--dbms_output.put_line('EXCEPTION0:'||SUBSTR(SQLERRM,?1,?100));
exit;
WHEN?OTHERS?THEN?
dbms_output.put_line('EXCEPTION1:'||SUBSTR(SQLERRM,?1,?100));
end;?
end?loop;?
--doc?:=?DBMS_XMLDOM.newDOMDocument(xmltype.extract(XMLType(l_clob),'/sqls/sql'));
cdata:=xmltype.extract(XMLType(l_clob),'/sqls/sql').getClobVal();
insert?into?inputfile(FILENAME,?XMLCONTENT,sqlcontent?)
values(pi_file_name,?XMLType(l_clob),replace(replace(replace(CDATA,'sql![CDATA[--',''),']]/sql',''),'/',chr(10)||'/'||chr(10)));
utl_file.fclose(filehandle);
IF?utl_file.is_open(filehandle)?THEN?
dbms_output.put_line('file?is?open!');?
else?
dbms_output.put_line('file?is?close!');?
END?IF;?
EXCEPTION?
WHEN?OTHERS?THEN?
dbms_output.put_line('EXCEPTION2:'||SUBSTR(SQLERRM,?1,?100))?;?
end;?
/
exec?alter_table('myTable.xml');
sql語句會去inputfile.sqlcontent.
循環(huán)取出值,然后execute?immediate(sql);
稍微做些微調(diào)即可。
Oracle執(zhí)行外部文件:
c:sqlplus
user/pwd@db
sql@new.sql
執(zhí)行多個(gè)sql文件:
1.把所有的文件都放在同一個(gè)目錄下,然后在命令行里執(zhí)行命令:
c:dir/b
d:/1.sql
會把所有的sql文件名都輸出到一個(gè)sql文件中。
2.用UltraEdit打開生成的sql文件,alt+C切換到column
mode,給所有的行前都添加一個(gè)"@",保存。
3.在sqlplus中執(zhí)行"@d:/1.sql"
如何避免''字符:
sql中可以傳遞參數(shù),但有時(shí)需要插入'',例:
SQL
select
'hello'
v
from
dual;
輸入
hello
的值:
hello
原值
1:
select
'hello'
v
from
dual
新值
1:
select
'hello'
v
from
dual
v
-----
hello
可以使用如下方法避免:
A:
SQL
select
chr(38)
||
'hello'
v
from
dual;
V
------
hello
B:
SQL
set
define
off
SQL
select
'hello'
v
from
dual;
V
------
hello