请教 一个bcp的写法,不够可以在加分

linyong 2003-05-16 10:30:40
在一个数据库中有两张表
table1
select * from table1
f1 f2 f3
1 aa nn
2 bb jg
table2
select * from table2
fa fb fc fd
2 123 aa bb
4 456 sd a

我希望使用bcp的导出功能
将两张表导出到一个txt文件中 格式如下
1 aa nn
2 bb jg
2 123 aa bb
4 456 sd a
然后在从这个txt的文件中导入到另一个数据库相同结构的表中。
请教bcp的导入、导出该如何写。
...全文
40 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
linyong 2003-05-16
  • 打赏
  • 举报
回复
如果一定要用一个文件呢,
导入的时候该怎么做呢。
以上的 还是没有答案。
wgy2008 2003-05-16
  • 打赏
  • 举报
回复


导出到DT.txt
EXEC master..xp_cmdshell 'bcp "select * from dbname..tablename " queryout c:\DT.txt -c -Sservername -Usa -Ppassword'
从DT.txt 导入:
EXEC master..xp_cmdshell 'bcp "dbname..tablename" in c:\DT.txt -c -Sservername -Usa -Ppassword'
happydreamer 2003-05-16
  • 打赏
  • 举报
回复
可以用union 联接两个查询

SELECT *,'' a FROM table1
UNION
SELECT * FROM table2

bcp "SELECT *,'' a FROM table1 UNION SELECT * FROM table2" queryout c:\test.txt -c -S server -U sa -P
yoki 2003-05-16
  • 打赏
  • 举报
回复
都说完了,没有要说的了
:)
愉快的登山者 2003-05-16
  • 打赏
  • 举报
回复
结构不同的数据,最好导到两个文件中。
导入时也好使用。
何必自找麻烦?
CrazyFor 2003-05-16
  • 打赏
  • 举报
回复
bcp "select *,'' as f4 from table1 union select * from table2" queryout AAA.txt" -c -q -U"SA" -P"password"
psxfghost 2003-05-16
  • 打赏
  • 举报
回复
drop #temp
select * into #temp from table2 (fa,fb,fc,fd) (select f1,f2,f3,'' as f4 from table1)

delete from table2
insert into table2 select * from #temp
然后用bcp导出
EXEC master..xp_cmdshell 'bcp table2 out c:\DT.txt -c -Sservername -Usa -Ppassword'

34,838

社区成员

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

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