简体   繁体   English

SQL Server 中的 bcp

[英]bcp in SQL Server

I want to use bcp in SQL Server to transfer data of a table to a text file.我想在 SQL Server 中使用bcp将表的数据传输到文本文件。

After that I want to truncate that table and move the transferred data from created text file to that table.之后,我想截断该表并将传输的数据从创建的文本文件移动到该表。

I want to check status (something like error status) to understand if there is are errors or exceptions when I move the data from table to text file, or move the data from text file to table (before and after I truncate the table) and rollback any thing and stop the process.我想检查状态(类似于错误状态)以了解当我将数据从表移动到文本文件或将数据从文本文件移动到表(截断表之前和之后)时是否存在错误或异常回滚任何事情并停止该过程。

How can I do that?我怎样才能做到这一点?

Here is the query这是查询

SET @sql = 'bcp "select * from [db].[table]" queryout "C:\textFile.txt" -c -T'
EXEC master..xp_cmdshell @sql

TRUNCATE [db].[table]

SET @sql = 'bcp [db].[table] in "C:\textFile.txt" -c -T -q'
EXEC master..xp_cmdshell @sql

Based on your comment, you should use another table rather than a text file:根据您的评论,您应该使用另一个表格而不是文本文件:

select *
into temp_table
from [db].[table];

truncate table [db].[table];  -- backup before you do this

-- do whatever changes you want

-- re-insert into the table

There is no need for the data to leave the database.数据无需离开数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM