The bcp query in sqlserver exports massive data

A few days ago, the company's ERP wanted to export data. Due to the large amount of data, it could not be exported by using jxl or poi export. Tomcat also set up something like virtual memory. Once the excel data was exported, the CPU utilization of the server was absolutely 100%. Hundreds, you must know that this is a dual-core 1.8G CPU, 1T hard disk, etc., and it is also a relatively famous server! The company's business is relatively complicated, and the sql statement is not dared to be changed. You must know that the greater the pressure, the greater the potential, the pressure is his mother, no, the pressure is like a spring, you are weak and he is strong! ^_^, go to Du Niang, see the use of bcp to export data, try it!

Let's take a look first, the usage of bcp: bcp{dbtable | query}{ in | out | queryout | format} data file

[ -m max number of errors ] [ -f format file ] [ -e error file ] [ -F first line ] [ -L last line ] [ -b batch size ] [ -n native type ] [ -c character type ] [ -w wide char type ] [ -N keep non-text as native type ] [ -V file format version ] [ -q quoted identifier ] [ -C code page specifier ] [ -t field terminator ] [ -r line terminator ] [ -i input file ] [ -o output file ] [ -a packet size ]





[ -S server name ] [ -U username ] [ -P password ] [ -T trusted connections ] [ -v version ] [ -R allow use of locale ] [ -k keep empty value ] [ -E keep identity value ] [ -h "load prompt" ] [ -x generate xml format file ]


First, install sql2005, sql2008 and other versions of sqlserver database on the server

Install sql2005, sql2008 and other versions of sqlserver database

Install sql2005, sql2008 and other versions of sqlserver database

Say important things 3 times ^_^

Next, you need to open the component 'xp_cmdshell', or report an error, because for security, sql is closed by default .

Error message: An error may be reported when executing the above command: Error message: Msg 15281, Level 16, State 1, Procedure xp_cmdshell, Line 1

When the graphical interface of sql2005 and sql2008 is opened, it will not be written. We use the statement format to open it.

-- allow configuration of advanced options
EXEC master.sys.sp_configure 'show advanced options', 1
-- reconfigure
RECONFIGURE
-- enable xp_cmdshell
EXEC master.sys.sp_configure 'xp_cmdshell', 1
-- reconfigure
RECONFIGURE

We only talk about one case of querying and exporting files in csv format. Other Baidu, (-t,) means that fields are connected with commas, and -T means secure connection

 

EXEC master..xp_cmdshell ' bcp "select username(column name 1),password(column name 2) from database name.dbo.table name" queryout c:\test.csv -c -t, -T '

Explain in general why it is recommended to export files in csv format, because for the convenience of calculation and analysis, most people like to use excel for statistical summary analysis, and excel has excel2003, excel2007, etc., the formats are different, and excel2003 can only display 65535 rows of data , and the csv file can be opened with excel for easy operation.

Someone said, can you add Chinese characters to the column name? It is absolutely possible, as follows

 

EXEC master..xp_cmdshell ' bcp " select ''username'', ''password'' union all
select username,password from 数据库名.dbo.表名" queryout c:\test.csv -c -t, -T'

又有人说了,如果导出的数据中有为空的话,csv文件会被截断,显示不全数据啊,没关系,假设密码有为空的用户,我们写成char(32),这个表示一个空格,再用excel打开csv文件时就不会显示不全数据,这是一个没有办法的办法,如果大家有好的方法,请贴出来告诉我一声,谢谢了!如下写法

 

 

EXEC master..xp_cmdshell 'bcp " select ''用户名'',''密码'' union all
select username,case when len(password)=0 then ''=char(32)'' else password end as password from 数据库名.dbo.表名" queryout c:\test.csv -c -t, -T'

 

还有人说了,我能不能根据查询条件导出csv数据啊,我还是回答,能,能,能,重要事情说3遍!

写存储过程啊!啊,你不会写,好吧,我好人当到底,给你,给你,都给你!!

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author:张述飞
-- Create date: 2016-04-13
-- Description:根据输入的查询条件导出csv文件
-- =============================================
create proc [dbo].[test]
@condition nvarchar(500)
as
declare @sql nvarchar(1000),
@result int
begin
SET NOCOUNT ON;
--允许配置高级选项
exec sp_configure 'show advanced options', '1';
--重新配置
reconfigure;
--启用xp_cmdshell
exec sp_configure 'xp_cmdshell', '1'
reconfigure;
set @sql = 'bcp "select ''用户名'', ''密码'' union all '+
'select username, '+
'(case when len(password)=0 then ''=char(32)'' else password end) as password '+
'from 数据库名.dbo.用户表名 where id > 0 '+@condition+'" queryout c:\test.csv -t"," -q -c -T'
--print @sql
exec @result = master..xp_cmdshell @sql, no_output
--允许配置高级选项
exec sp_configure 'show advanced options', '1';
--重新配置
reconfigure;
--关闭xp_cmdshell
exec sp_configure 'xp_cmdshell', '0';
reconfigure;
select @result as failrows
end

创建完了存储过程,我们测试一下
declare @condition nvarchar(500)
set @condition=' and username like ''zhang%'''
exec test @condition

打印结果为0,说明导出成功了!

基本上我就写到这里了,至于程序于怎么调用,大家多动手试试就行了,^_^



Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327040068&siteId=291194637