急救

caiyun 2003-11-24 11:02:13
Sql server 怎样用语句导出*.txt文件(包括字段名称)?
为什么*.txt文件导入到Sql server 中数据类型会改变,怎么做不改变?
帮帮忙了!
...全文
47 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
SunnyJing0201 2003-11-24
  • 打赏
  • 举报
回复
完全可以的导入导出工具是SQL中一个很强大的功能
试试很好用的
youngby 2003-11-24
  • 打赏
  • 举报
回复
Create Procedure prBcpOutTables
@debug int=0

as

declare @chvTable varchar(100),
@chvCommand varchar(300)

declare @curTables Cursor

set @curTables=Cursor for
select name from sysobjects
where xType='u'

Open #curTables


--get first table
Fetch next from @curTables
into @chvTable

While(@@fetch_status=0)
Begin
set @chvCommand='bcp "myDataBase..['+@chvTable +']" out c:\sql\backup\'+ @chvTable+'.txt -c -a -Sservername -Usa -Ppassword'

if @debug<>0
select @chvCommand chvCommmand
if @debug=0
execute xp_command @chvCommand,no_output


fetch next from @curTables
into @chvTable
end
close @curTables
Deallocate @curTables
return 0

caiyun 2003-11-24
  • 打赏
  • 举报
回复
用语句?
lvltt 2003-11-24
  • 打赏
  • 举报
回复
DTS导出导入
txlicenhe 2003-11-24
  • 打赏
  • 举报
回复
1:企业管理器DTS全向导操作
2:
http://expert.csdn.net/Expert/topic/2341/2341997.xml?temp=.2739221
SQL语句导入导出大全
viptiger 2003-11-24
  • 打赏
  • 举报
回复
EXEC master..xp_cmdshell 'bcp "SELECT au_fname, au_lname FROM pubs..authors ORDER BY au_lname" Authors.txt -c -Sservername -Usa -Ppassword'
pengdali 2003-11-24
  • 打赏
  • 举报
回复
你先导到一个中间表
再用:
insert 你的目标表 select * from 中间表
pengdali 2003-11-24
  • 打赏
  • 举报
回复
CREATE proc out2xls
@服务器名 varchar(255),
@库名 varchar(255),
@表名 varchar(255),
@用户名 varchar(100),
@密码 varchar(100),
@路径及文件名 varchar(255)
as
declare @temp1 nvarchar(4000),@temp2 varchar(8000)

set @temp1='select @value1='''',@value2='''' select @value1=@value1+'',''''''+a.name+''''+char(39)+'' [''+a.name+'']'',@value2=@value2+'',cast(''+''[''+a.name+'']''+ '' as varchar(200))'' from '+@库名+'..syscolumns a,'+@库名+'..sysobjects d where a.id=d.id and d.name='''+@表名+''''+' order by a.colorder'


exec sp_executesql @temp1,N'@value1 nvarchar(4000) output , @value2 varchar(8000) output',@temp1 output,@temp2 output

select @temp1=right(@temp1,len(@temp1)-1),@temp2=right(@temp2,len(@temp2)-1)

exec('select * into '+@库名+'.dbo.中间表 from (select '+ @temp1+' union all SELECT '+@temp2+' FROM '+@库名+'..'+@表名+') tem3')


set @temp2='bcp '+@库名+'.dbo.中间表 out '+@路径及文件名+' -c -S'+@服务器名+' -U'+@用户名+' -P'+@密码

EXEC master..xp_cmdshell @temp2
exec('drop table '+@库名+'.dbo.中间表')
GO


exec out2xls 'daliserver','pubs','jobs','sa','element','c:\a.txt'
go
drop proc out2xls
viptiger 2003-11-24
  • 打赏
  • 举报
回复
可以用企业管理其中的DTS工具作导入导出,非常好用

34,874

社区成员

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

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