,如何把txt文本数据导入SQL Server2005数据库中

如题所述

第1个回答  2019-02-24
/**
导入文本文件
EXEC
master..xp_cmdshell
'bcp
dbname..tablename
in
c:\DT.txt
-c
-Sservername
-Usa
-Ppassword'
/**
导出文本文件
EXEC
master..xp_cmdshell
'bcp
dbname..tablename
out
c:\DT.txt
-c
-Sservername
-Usa
-Ppassword'

EXEC
master..xp_cmdshell
'bcp
"Select
*
from
dbname..tablename"
queryout
c:\DT.txt
-c
-Sservername
-Usa
-Ppassword'
导出到TXT文本,用逗号分开
exec
master..xp_cmdshell
'bcp
"库名..表名"
out
"d:\tt.txt"
-c
-t
,-U
sa
-P
password'
BULK
INSERT
库名..表名
FROM
'c:\test.txt'
WITH
(
FIELDTERMINATOR
=
';',
ROWTERMINATOR
=
'\n'
)
第2个回答  2020-01-09
1.先生成个空的csv文件(不要只是把txt文件的后缀改为csv,这样容易导致异常)
2.先把txt中各个项目的间隔符替换为刚才做成的csv文件的分隔符,再拷贝到做成的csv文件中
3.做一个bat,执行sql文,把文件读到数据库中
或者用SQL
Server2005自带的导入功能
吧txt文件直接拷贝到excel中容易出现数据不一致的问题(比如说日期等)
第3个回答  2019-06-14
下面给出直接代码如下:
--第一步:启用Ad Hoc Distributed Queries:
exec sp_
configure
'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
--第二步:bcp导入数据
EXEC master..xp_cmdshell 'bcp dbname..tablename in c:\DT.txt -c -Sservername -Usa -Ppassword'
--第三步:关闭Ad Hoc Distributed Queries:
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure
第4个回答  2019-09-19
--第一步:启用ad
hoc
distributed
queries:
exec
sp_configure
'show
advanced
options',1
reconfigure
exec
sp_configure
'ad
hoc
distributed
queries',1
reconfigure
--第二步:bcp导入数据
exec
master..xp_cmdshell
'bcp
dbname..tablename
in
c:\dt.txt
-c
-sservername
-usa
-ppassword'
--第三步:关闭ad
hoc
distributed
queries:
exec
sp_configure
'ad
hoc
distributed
queries',0
reconfigure
exec
sp_configure
'show
advanced
options',0
reconfigure
相似回答
大家正在搜