t sql讀寫檔案

2021-08-07 11:34:25 字數 1959 閱讀 3063

declare @filepath nvarchar(400),@xmlstr varchar(8000);

declare @int_err int ----錯誤**,0 成功

declare @int_fso int ----fso物件標誌

declare @int_openfile int ----文字檔案物件標誌

declare @str_content varchar(8000) ----檔案內容

declare @output varchar(255)

declare @hr int

declare @source varchar(255)

declare @description varchar(255)

select @filepath = logid from optlog;

set @filepath = 'f:/' + @filepath + '.xml';

--set @filepath='f:/mp.xml'

--set @str_content = @xmlstr

select @str_content = xmlinfo from optlog;

--建立fso物件

--甚至可以是我們自己寫的dll控制項都可以

exec @int_err = sp_oacreate 'scripting.filesystemobject', @int_fso output

if(@int_err <> 0)

begin

exec sp_oageterrorinfo @int_fso

return

end-----檔案路徑

declare @str_filename nvarchar(50)

set @str_filename = @filepath

---fso.opentextfile(filename, 1, false),第二個引數1表示唯讀,第三個引數false表示檔案不存在時不建立

----建立文字檔案物件

----fso的所有方法都可以用下面類似方式建立,可以實現檔案的任意操作,非常方便

exec @int_err=sp_oamethod @int_fso,'createtextfile',@int_openfile output,@str_filename

if(@int_err <> 0)

begin

exec sp_oageterrorinfo @int_openfile,@source out, @description out

select @output = ' 

source: ' + @source

print @output

select @output = ' 

description: ' + @description

print @output

return

end----讀取檔案,內容放到@str_content

----讀到了檔案內容,後面的處理就好辦了

exec @int_err=sp_oamethod @int_openfile,'write',null,@str_content

if(@int_err <> 0)

begin

exec sp_oageterrorinfo @int_fso, @description out

select @output = ' 

source: ' + @source

print @output

select @output = ' 

description: ' + @description

print @output

return

end----銷毀建立的物件

exec @int_err=sp_oadestroy @int_openfile

exec @int_err=sp_oadestroy @int_fso

T SQL 讀取磁碟檔案

資料倒入還是建議用bcp ifexists select fromtempdb.sysobjectswhereid object id n tempdb.t droptable t createtable t idintidentity 1,1 notnull,remarkvarchar 8001 ...

c 檔案讀寫 文字讀寫

include int main else return 0 格式 intfscanf file stream,constchar format,返回值 如果成功,該函式返回成功匹配和賦值的個數。如果到達檔案末尾或發生讀錯誤,則返回 eof 引數1 file stream 檔案指標 引數2 cons...

mysql讀寫檔案函式 讀寫檔案 檔案方法 函式

讀寫檔案 全域性申明 import codecs encodeing utf 8 開啟檔案的模式有 r,唯讀模式 預設 w,只寫模式。不可讀 不存在則建立 存在則刪除內容 a,追加模式。可讀 不存在則建立 存在則只追加內容 表示可以同時讀寫某個檔案 r 可讀寫檔案。可讀 可寫 可追加 w 寫讀 a ...