△如何操作服务器文件?

蓝色光芒 2003-09-22 03:01:44
如何用SQL语句,把SQL里面某个表里的内容保存为服务器上的某个文件?

如:有一个表Test,
Test里有一个字段 Memo[LongText]
如何把在这个字段里的某一条记录的Memo字段的内容保存到服务器的
C:\Memo.Text
??
谢谢!
...全文
29 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
subbee 2003-09-23
  • 打赏
  • 举报
回复
UP
蓝色光芒 2003-09-22
  • 打赏
  • 举报
回复
谢谢各位,另想它法!!
zjcxc 元老 2003-09-22
  • 打赏
  • 举报
回复
那就没办法啦,除非你在sql server服务器上建asp服务器,用asp来实现.
蓝色光芒 2003-09-22
  • 打赏
  • 举报
回复
zjcxc(邹建)
大哥,你的办法确实很好,感谢!
但是我担心很多SQL为了安全都把xplog70.dll删除了,就用不起xp_cmdshell了,该怎么办啦?
zjcxc 元老 2003-09-22
  • 打赏
  • 举报
回复
sql server中没有LongText这个类型,如果是字符型,可以用上面的存储过程,如果是text或者ntext类型,可以用下面的存储过程.

/*--bcp-二进制文件的导入导出

支持image,text,ntext字段的导入/导出
image适合于二进制文件;text,ntext适合于文本数据文件

注意:导入时,将覆盖满足条件的所有行
导出时,将把所有满足条件的行也出到指定文件中

此存储过程仅用bcp实现
邹建 2003.08-----------------*/

/*--调用示例
--数据导出
exec p_binaryIO 'zj','','','acc_演示数据..test','memo','c:\memo.txt'

--数据导出
exec p_binaryIO 'zj','','','acc_演示数据..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),--服务器名称
@username varchar (30), --用户名
@password varchar (30), --密码
@tbname varchar (500), --数据库..表名
@fdname varchar (30), --字段名
@fname varchar (1000), --目录+文件名,处理过程中要使用/覆盖:@filename+.bak
@tj varchar (1000)='', --处理条件.对于数据导入,如果条件中包含@fdname,请指定表名前缀
@isout bit=1 --1导出((默认),0导入
AS
declare @fname_in varchar(1000) --bcp处理应答文件名
,@fsize varchar(20) --要处理的文件的大小
,@m_tbname varchar(50) --临时表名
,@sql varchar(8000)

--则取得导入文件的大小
if @isout=1
set @fsize='0'
else
begin
create table #tb(可选名 varchar(20),大小 int
,创建日期 varchar(10),创建时间 varchar(20)
,上次写操作日期 varchar(10),上次写操作时间 varchar(20)
,上次访问日期 varchar(10),上次访问时间 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

--生成数据处理应答文件
set @m_tbname='[##temp'+cast(newid() as varchar(40))+']'
set @sql='select * into '+@m_tbname+' from(
select null as 类型
union all select 0 as 前缀
union all select '+@fsize+' as 长度
union all select null as 结束
union all select null as 格式
) a'
exec(@sql)
select @fname_in=@fname+'_temp'
,@sql='bcp "'+@m_tbname+'" out "'+@fname_in
+'" /S"'+@servename
+case when isnull(@username,'')='' then ''
else '" /U"'+@username end
+'" /P"'+isnull(@password,'')+'" /c'
exec master..xp_cmdshell @sql
--删除临时表
set @sql='drop table '+@m_tbname
exec(@sql)

if @isout=1
begin
set @sql='bcp "select top 1 '+@fdname+' from '
+@tbname+case isnull(@tj,'') when '' then ''
else ' where '+@tj end
+'" queryout "'+@fname
+'" /S"'+@servename
+case when isnull(@username,'')='' then ''
else '" /U"'+@username end
+'" /P"'+isnull(@password,'')
+'" /i"'+@fname_in+'"'
exec master..xp_cmdshell @sql
end
else
begin
--为数据导入准备临时表
set @sql='select top 0 '+@fdname+' into '
+@m_tbname+' from ' +@tbname
exec(@sql)

--将数据导入到临时表
set @sql='bcp "'+@m_tbname+'" in "'+@fname
+'" /S"'+@servename
+case when isnull(@username,'')='' then ''
else '" /U"'+@username end
+'" /P"'+isnull(@password,'')
+'" /i"'+@fname_in+'"'
exec master..xp_cmdshell @sql

--将数据导入到正式表中
set @sql='update '+@tbname
+' set '+@fdname+'=b.'+@fdname
+' from '+@tbname+' a,'
+@m_tbname+' b'
+case isnull(@tj,'') when '' then ''
else ' where '+@tj end
exec(@sql)

--删除数据处理临时表
set @sql='drop table '+@m_tbname
end

--删除数据处理应答文件
set @sql='del '+@fname_in
exec master..xp_cmdshell @sql

go
zjcxc 元老 2003-09-22
  • 打赏
  • 举报
回复
调用示例有点错误,改正如下:

/*--调用示例
导出调用示例
--导出指定表,这里指定导出表:地区资料
exec file2table 'zj','','','c:\zj.txt','xzkh_sa..地区资料'

导入调用示例
--导入指定表,这里指定导出表:地区资料
exec file2table 'zj','','','c:\zj.txt','xzkh_sa..地区资料',0
--*/
伍子V5 2003-09-22
  • 打赏
  • 举报
回复
参考一下了

方法:
1、建立过程
CREATE PROCEDURE sp_textcopy (
@srvname varchar (30),
@login varchar (30),
@password varchar (30),
@dbname varchar (30),
@tbname varchar (30),
@colname varchar (30),
@filename varchar (30),
@whereclause varchar (40),
@direction char(1))
AS
DECLARE @exec_str varchar (255)
SELECT @exec_str =
'textcopy /S ' + @srvname +
' /U ' + @login +
' /P ' + @password +
' /D ' + @dbname +
' /T ' + @tbname +
' /C ' + @colname +
' /W "' + @whereclause +
'" /F ' + @filename +
' /' + @direction
EXEC master..xp_cmdshell @exec_str

2、建表和初始化数据
create table 表名 (编号 int,image列名 image)
go
insert 表名 values(1,0x)
insert 表名 values(2,0x)
go

3、读入
sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\图片.bmp','where 编号=1','I' --注意条件是 编号=1

sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\bb.doc','where 编号=2','I' --注意条件是 编号=2

go

4、读出成文件
sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\图片.bmp','where 编号=1','O' --注意条件是 编号=1

sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\bb.doc','where 编号=2','O' --注意条件是 编号=2
go
蓝色光芒 2003-09-22
  • 打赏
  • 举报
回复
哦,一楼说的是:TextCopy.Exe
可行!!!
但不知道他是怎么操作SQL的。
Wally_wu 2003-09-22
  • 打赏
  • 举报
回复
使用DTS
zjcxc 元老 2003-09-22
  • 打赏
  • 举报
回复
用这个存储过程就可以搞定啦.



/*--实现数据导入/导出的存储过程

可以实现导入/导出 指定表 到文本文件

邹建 2003.07-----------------*/

/*--调用示例
导出调用示例
--导出指定表,这里指定导出表:地区资料 和 基本信息,文件名前缀为:zj
exec file2table 'zj','','','c:\zj.txt','xzkh_sa..地区资料'

导入调用示例
--导入指定表,这里指定导出表:地区资料 和 基本信息,文件名前缀为:zj
exec file2table 'zj','','','c:\zj.txt','xzkh_sa..地区资料',0
--*/

if exists(select 1 from sysobjects where name='File2Table' and objectproperty(id,'IsProcedure')=1)
drop procedure File2Table
go

create procedure File2Table
@servername varchar(200) --服务器名
,@username varchar(200) --用户名,如果用NT验证方式,则为空''
,@password varchar(200) --密码
,@filename varchar(1000) --目录名+文件名
,@tbname varchar(500)='' --数据库..表名
,@isout bit=1 --1为导出(默认),0为导入
as
declare @sql varchar(8000)

set @sql='bcp "'+@tbname
+case when @isout=1 then '" out' else '" in' end
+' "'+@filename+'" /w' +' /S"'+@servername
+case when isnull(@username,'')='' then ''
else '" /U"'+@username end
+'" /P"'+isnull(@password,'')+'"'
exec master..xp_cmdshell @sql
go
happydreamer 2003-09-22
  • 打赏
  • 举报
回复
bcp支持导出text字段的内容

EXEC master..xp_cmdshell 'bcp "select Memo from dbname..test where..." queryout c:\memo.txt -c -S server -U -P '
happydreamer 2003-09-22
  • 打赏
  • 举报
回复
bcp支持导出text字段的内容

EXEC master..xp_cmdshell 'bcp "select Memo from dbname..test where..." queryout c:\memo.txt -c -S server -U -P '
蓝色光芒 2003-09-22
  • 打赏
  • 举报
回复
sdhdy(大江东去...) 这个方法不行,对服务器的依赖太强,需要xplog70.dll支持。
TextCopy SQL里面没有帮助说明,晕,
有例子吗?
sdhdy 2003-09-22
  • 打赏
  • 举报
回复
--query
EXEC master..xp_cmdshell 'bcp "select Memo from 你的数据库名..test" queryout c:\Memo.txt -c -S servername -U sa -P '
whidon 2003-09-22
  • 打赏
  • 举报
回复
TEXTCOPY [/S [sqlserver]] [/U [login]] [/P [password]]
[/D [database]] [/T table] [/C column] [/W"where clause"]
[/F file] [{/I | /O}] [/K chunksize] [/Z] [/?]

/S sqlserver The SQL Server to connect to. If 'sqlserver' is not
specified, the local SQL Server is used.
/U login The login to connect with. If 'login' is not specified,
a trusted connection will be used.
/P password The password for 'login'. If 'password' is not
specified, a NULL password will be used.
/D database The database that contains the table with the text or
image data. If 'database' is not specified, the default
database of 'login' is used.
/T table The table that contains the text or image value.
/C column The text or image column of 'table'.
/W "where clause" A complete where clause (including the WHERE keyword)
that specifies a single row of 'table'.
/F file The file name.
/I Copy text or image value into SQL Server from 'file'.
/O Copy text or image value out of SQL Server into 'file'.
/K chunksize Size of the data transfer buffer in bytes. Minimum
value is 1024 bytes, default value is 4096 bytes.
/Z Display debug information while running.
/? Display this usage information and exit.

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧