sql數(shù)據(jù)庫(kù)中用image來存儲(chǔ)文件,但sql沒有提供直接的存取文件的命令.
/*--bcp 實(shí)現(xiàn)二進(jìn)制文件的導(dǎo)入導(dǎo)出
支持image,text,ntext字段的導(dǎo)入/導(dǎo)出
image適合于二進(jìn)制文件,包括:word文檔,excel文檔,圖片,音樂等
text,ntext適合于文本數(shù)據(jù)文件
注意:導(dǎo)入時(shí),將覆蓋滿足條件的所有行
導(dǎo)出時(shí),將把所有滿足條件的行導(dǎo)出到指定文件中
此存儲(chǔ)過程僅用bcp實(shí)現(xiàn)
-----------------*/
/*--調(diào)用示例
--數(shù)據(jù)導(dǎo)出
exec p_binaryio 'zj','','','acc_演示數(shù)據(jù)..tb','img','c:/zj1.dat'
--數(shù)據(jù)導(dǎo)入
exec p_binaryio 'zj','','','acc_演示數(shù)據(jù)..tb','img','c:/zj1.dat','',0
--*/
if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[p_binaryio]') and objectproperty(id, n'isprocedure') = 1)
drop procedure [dbo].[p_binaryio]
go
create proc p_binaryio
@servename varchar (30),--服務(wù)器名稱
@username varchar (30), --用戶名
@password varchar (30), --密碼
@tbname varchar (500), --數(shù)據(jù)庫(kù)..表名
@fdname varchar (30), --字段名
@fname varchar (1000), --目錄+文件名,處理過程中要使用/覆蓋:@filename+_temp
@tj varchar (1000)='', --處理?xiàng)l件.對(duì)于數(shù)據(jù)導(dǎo)入,如果條件中包含@fdname,請(qǐng)指定表名前綴
@isout bit=1 --1導(dǎo)出((默認(rèn)),0導(dǎo)入
as
declare @fname_in varchar(1000) --bcp處理應(yīng)答文件名
,@fsize varchar(20) --要處理的文件的大小
,@m_tbname varchar(50) --臨時(shí)表名
,@sql varchar(8000)
--則取得導(dǎo)入文件的大小
if @isout=1
set @fsize='0'
else
begin
create table #tb(可選名 varchar(20),大小 int
,創(chuàng)建日期 varchar(10),創(chuàng)建時(shí)間 varchar(20)
,上次寫操作日期 varchar(10),上次寫操作時(shí)間 varchar(20)
,上次訪問日期 varchar(10),上次訪問時(shí)間 varchar(20),特性 int)
insert into #tb
exec master..xp_getfiledetails @fname
select @fsize=大小 from #tb
drop table #tb
if @fsize is null
begin
print '文件未找到'
return
end
end
--生成數(shù)據(jù)處理應(yīng)答文件
set @m_tbname='[##temp'+cast(newid() as varchar(40))+']'
set @sql='select * into '[email protected]_tbname+' from(
select null as 類型
union all select 0 as 前綴
union all select '[email protected]+' as 長(zhǎng)度
union all select null as 結(jié)束
union all select null as 格式
) a'
exec(@sql)
select @[email protected]+'_temp'
,@sql='bcp "'[email protected]_tbname+'" out "'[email protected]_in
+'" /s"'[email protected]
+case when isnull(@username,'')='' then ''
else '" /u"'[email protected] end
+'" /p"'+isnull(@password,'')+'" /c'
exec master..xp_cmdshell @sql
--刪除臨時(shí)表
set @sql='drop table '[email protected]_tbname
exec(@sql)
if @isout=1
begin
set @sql='bcp "select top 1 '[email protected]+' from '
[email protected]+case isnull(@tj,'') when '' then ''
else ' where '[email protected] end
+'" queryout "'[email protected]
+'" /s"'[email protected]
+case when isnull(@username,'')='' then ''
else '" /u"'[email protected] end
+'" /p"'+isnull(@password,'')
+'" /i"'[email protected]_in+'"'
exec master..xp_cmdshell @sql
end
else
begin
--為數(shù)據(jù)導(dǎo)入準(zhǔn)備臨時(shí)表
set @sql='select top 0 '[email protected]+' into '
[email protected]_tbname+' from ' [email protected]
exec(@sql)
--將數(shù)據(jù)導(dǎo)入到臨時(shí)表
set @sql='bcp "'[email protected]_tbname+'" in "'[email protected]
+'" /s"'[email protected]
+case when isnull(@username,'')='' then ''
else '" /u"'[email protected] end
+'" /p"'+isnull(@password,'')
+'" /i"'[email protected]_in+'"'
exec master..xp_cmdshell @sql
--將數(shù)據(jù)導(dǎo)入到正式表中
set @sql='update '[email protected]
+' set '[email protected]+'=b.'[email protected]
+' from '[email protected]+' a,'
[email protected]_tbname+' b'
+case isnull(@tj,'') when '' then ''
else ' where '[email protected] end
exec(@sql)
--刪除數(shù)據(jù)處理臨時(shí)表
set @sql='drop table '[email protected]_tbname
end
--刪除數(shù)據(jù)處理應(yīng)答文件
set @sql='del '[email protected]_in
exec master..xp_cmdshell @sql
go
網(wǎng)站運(yùn)營(yíng)seo文章大全提供全面的站長(zhǎng)運(yùn)營(yíng)經(jīng)驗(yàn)及seo技術(shù)!