use EntDataCenter
go
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
--exec dbo.ent_all_DataToExcel
create PROCEDURE ent_all_DataToExcel AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT off;
DECLARE @str2name varchar(8000);--二级国代分类名称
DECLARE @str2code varchar(8000);--二级国代分类代码
DECLARE @str1name varchar(8000);--一级国代分类名称
DECLARE @str varchar(8000);
DECLARE @str1 varchar(8000);
DECLARE @server varchar(100);--服务器
DECLARE @uname varchar(100);--用户名
DECLARE @pwd varchar(100);--密码 DECLARE @IsExist bit;
select @str2name='';
select @str2code='';
select @str1name='';
select @str='';
select @str1='';
set @server='192.168.1.7';
set @uname='dev_db';
set @pwd='dev_db';
set @IsExist=0; ---判断文件夹是否存在,若不存在则创建文件夹----
begin
CREATE TABLE #tmp ([File Exists] BIT, [File is a Directory] BIT, [Parent Directory Exists] BIT)
INSERT INTO #tmp ([File Exists], [File is a Directory], [Parent Directory Exists])
EXEC master.dbo.xp_fileexist 'c:\project';
SELECT @IsExist=[File is a Directory] FROM #tmp;
if(@IsExist=0)
begin
ExEc xp_cmdshell 'mkdir c:\project';
end
drop table #tmp
end DECLARE contact_cursor CURSOR FOR (SELECT a.国代分类名称 as 二级国代分类名称,a.国代分类代码 as 二级国代分类代码,b.国代分类名称 as 一级国代分类名称
FROM [EntDataCenter].[dbo].[doc_class_nation_code] a
inner join [EntDataCenter].[dbo].[doc_class_nation_code] b
on a.PCode=b.国代分类代码 where a.Level=1)
OPEN contact_cursor
FETCH NEXT FROM contact_cursor
INTO @str2name,@str2code,@str1name
WHILE @@FETCH_STATUS = 0
BEGIN
begin
set @str='select a.[企业名称],a.[所在省份],a.[所在城市],' ;
set @str=@str+'(case b.year when 2005 then b.[total_assets] else 0 end) as [2005资产],';
set @str=@str+'(case b.year when 2006 then b.[total_assets] else 0 end) as [2006资产],';
set @str=@str+'(case b.year when 2007 then b.[total_assets] else 0 end) as [2007资产],';
set @str=@str+'(case b.year when 2008 then b.[total_assets] else 0 end) as [2008资产],';
set @str=@str+'(case b.year when 2009 then b.[total_assets] else 0 end) as [2009资产],';
set @str=@str+'(case b.year when 2010 then b.[total_assets] else 0 end) as [2010资产],';
set @str=@str+'(case b.year when 2011 then b.[total_assets] else 0 end) as [2011资产],';
set @str=@str+'(case b.year when 2012 then b.[total_assets] else 0 end) as [2012资产],';
set @str=@str+'(case b.year when 2005 then b.total_current_assets else 0 end) as [2005流动资产],';
set @str=@str+'(case b.year when 2006 then b.total_current_assets else 0 end) as [2006流动资产],';
set @str=@str+'(case b.year when 2007 then b.total_current_assets else 0 end) as [2007流动资产],';
set @str=@str+'(case b.year when 2008 then b.total_current_assets else 0 end) as [2008流动资产],';
set @str=@str+'(case b.year when 2009 then b.total_current_assets else 0 end) as [2009流动资产],';
set @str=@str+'(case b.year when 2010 then b.total_current_assets else 0 end) as [2010流动资产],';
set @str=@str+'(case b.year when 2011 then b.total_current_assets else 0 end) as [2011流动资产],';
set @str=@str+'(case b.year when 2012 then b.total_current_assets else 0 end) as [2012流动资产],';
set @str=@str+'(case b.year when 2005 then b.total_liabilities else 0 end) as [2005负债],';
set @str=@str+'(case b.year when 2006 then b.total_liabilities else 0 end) as [2006负债],';
set @str=@str+'(case b.year when 2007 then b.total_liabilities else 0 end) as [2007负债],';
set @str=@str+'(case b.year when 2008 then b.total_liabilities else 0 end) as [2008负债],';
set @str=@str+'(case b.year when 2009 then b.total_liabilities else 0 end) as [2009负债],';
set @str=@str+'(case b.year when 2010 then b.total_liabilities else 0 end) as [2010负债],';
set @str=@str+'(case b.year when 2011 then b.total_liabilities else 0 end) as [2011负债],';
set @str=@str+'(case b.year when 2012 then b.total_liabilities else 0 end) as [2012负债],';
set @str=@str+'(case b.year when 2005 then b.operating_income else 0 end) as [2005收入],';
set @str=@str+'(case b.year when 2006 then b.operating_income else 0 end) as [2006收入],';
set @str=@str+'(case b.year when 2007 then b.operating_income else 0 end) as [2007收入],';
set @str=@str+'(case b.year when 2008 then b.operating_income else 0 end) as [2008收入],';
set @str=@str+'(case b.year when 2009 then b.operating_income else 0 end) as [2009收入],';
set @str=@str+'(case b.year when 2010 then b.operating_income else 0 end) as [2010收入],';
set @str=@str+'(case b.year when 2011 then b.operating_income else 0 end) as [2011收入],';
set @str=@str+'(case b.year when 2012 then b.operating_income else 0 end) as [2012收入],';
set @str=@str+'(case b.year when 2005 then b.operating_costs else 0 end) as [2005成本],';
set @str=@str+'(case b.year when 2006 then b.operating_costs else 0 end) as [2006成本],';
set @str=@str+'(case b.year when 2007 then b.operating_costs else 0 end) as [2007成本],';
set @str=@str+'(case b.year when 2008 then b.operating_costs else 0 end) as [2008成本],';
set @str=@str+'(case b.year when 2009 then b.operating_costs else 0 end) as [2009成本],';
set @str=@str+'(case b.year when 2010 then b.operating_costs else 0 end) as [2010成本],';
set @str=@str+'(case b.year when 2011 then b.operating_costs else 0 end) as [2011成本],';
set @str=@str+'(case b.year when 2012 then b.operating_costs else 0 end) as [2012成本],';
set @str=@str+'(case b.year when 2005 then b.total_profit else 0 end) as [2005利润总额],';
set @str=@str+'(case b.year when 2006 then b.total_profit else 0 end) as [2006利润总额],';
set @str=@str+'(case b.year when 2007 then b.total_profit else 0 end) as [2007利润总额],';
set @str=@str+'(case b.year when 2008 then b.total_profit else 0 end) as [2008利润总额],';
set @str=@str+'(case b.year when 2009 then b.total_profit else 0 end) as [2009利润总额],';
set @str=@str+'(case b.year when 2010 then b.total_profit else 0 end) as [2010利润总额],';
set @str=@str+'(case b.year when 2011 then b.total_profit else 0 end) as [2011利润总额],';
set @str=@str+'(case b.year when 2012 then b.total_profit else 0 end) as [2012利润总额]';
set @str=@str+'from [dbo].[ent_all] a ';
set @str=@str+'left join [dbo].[Ent_All_Finance] b on a.ent_id = b.ent_id where a.国代分类代码 like '''+@str2code+'%'''; --set @str='select * from [EntDataCenter].[dbo].[doc_class_nation_code] where Level=1';
set @str1='bcp "'+@str+'" queryout C:\project\'+@str1name+'-'+@str2name+'.xls -c -S'+@server+' -U'+@uname+' -P'+@pwd+'';
end
--print @str1;
EXEC master..xp_cmdshell @str1 FETCH NEXT FROM contact_cursor
INTO @str2name,@str2code,@str1name
END
CLOSE contact_cursor
DEALLOCATE contact_cursor END
GO

sqlserver 脚本方式导出数据到excel的更多相关文章

  1. 使用python脚本从数据库导出数据到excel

    python从数据库导出数据到excel 最近需要从数据库里导出一些数据到excel,刚开始我是使用下面的命令 select * from xxx where xxx into outfile 'xx ...

  2. 使用Open xml 操作Excel系列之二--从data table导出数据到Excel

    由于Excel中提供了透视表PivotTable,许多项目都使用它来作为数据分析报表. 在有些情况下,我们需要在Excel中设计好模板,包括数据源表,透视表等, 当数据导入到数据源表时,自动更新透视表 ...

  3. ASP.NET导出数据到Excel 实例介绍

    ASP.NET导出数据到Excel  该方法只是把asp.net页面保存成html页面只是把后缀改为xlc不过excel可以读取,接下连我看看还有别的方式能导出数据,并利用模版生成. 下面是代码 新建 ...

  4. python 导出数据到excel 中,一个好用的导出数据到excel模块,XlsxWriter

    最近公司有项目需要导出数据到excel,首先想到了,tablib,xlwt,xlrd,xlwings,win32com[还可以操作word],openpyxl,等模块但是 实际操作中tablib 写入 ...

  5. Delphi 导出数据至Excel的7种方法【转】

    一; delphi 快速导出excel   uses ComObj,clipbrd;   function ToExcel(sfilename:string; ADOQuery:TADOQuery): ...

  6. 微软BI 之SSIS 系列 - 导出数据到 Excel 2013 的实现

    开篇介绍 碰到有几个朋友问到这个问题,比较共性,就特意写了这篇小文章说明一下如何实现在 SSIS 中导出数据到 Office Excel 2013 中.通常情况下 2013 以前的版本大多没有问题,但 ...

  7. phpexcel如何读取excel的数据和如何导出数据到excel

    phpexcel如何读取excel的数据和如何导出数据到excel 一.总结 一句话总结:去官网看参考手册和api,或者找中文的博客或者参考手册 1.phpexcel插件如何下载? 其实这些插件不仅可 ...

  8. 从DataTable高效率导出数据到Excel

    首先从数据库读取数据到DataTable,这我就不提了,大家都明白.下面直接介绍如何从DataTable高效率导出数据到Excel中的方法,代码如下: using Microsoft.Office.I ...

  9. Delphi 导出数据至Excel的7种方法

    一; delphi 快速导出excel uses ComObj,clipbrd; function ToExcel(sfilename:string; ADOQuery:TADOQuery):bool ...

随机推荐

  1. lucene特殊字符处理

    这是个郁闷的问题,今天遇到了,但在lucene中查询的关键字保护有特殊字符,譬如--,会出现如下异常: org.apache.lucene.queryParser.ParseException: Ca ...

  2. CentOS下将自编译的Apache添加为系统服务

    首先,先谈下对linux服务的理解 1,linux 服务运行方式: 脚本的方式运行,服务脚本存放位置/etc/rc.d/init.d/ 2,linux服务管理软件 chkconfig Red Hat公 ...

  3. IDL 计算TVDI

    介绍请看:http://blog.sina.com.cn/s/blog_764b1e9d0100wdrr.html 源码: IDL 源码PRO TVDI,NDVI,LST,NBINS,RES RES ...

  4. HDU 5480 Conturbatio

    区间求和不更新,开个数组记录一下前缀和就可以了 #include<cstdio> #include<cstring> #include<cmath> #includ ...

  5. java导读

    导读:      我们学习Java大概有3个方向,第一,桌面系统,包括C/S结构:第二,J2ME,面向无限领域,很有潜力的家伙,看看中国的手机用户就知道了.第三,面向企业应用.计算的平台,J2EE. ...

  6. web.xml中常用元素的解读

    前言 针对一个项目而言,通常会有几类XML文件需要书写. web.xml spring-context.xml spring-mvc.xml other.xml ... 不管有多少配置文件,可以肯定的 ...

  7. jQuery两种扩展插件的方式

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  8. ab测试 uwsgi遇到的问题

    1 请求并发数目较大时,接收到的数目小于发送的数目 1.1 描述:uwsgi正常返回302跳转 ab -n 5000 -c 250 -g test.log "192.168.50.20:90 ...

  9. SpringMVC强大的数据绑定

    6.6.2.@RequestParam绑定单个请求参数值 @RequestParam用于将请求参数区数据映射到功能处理方法的参数上. public String requestparam1(@Requ ...

  10. 使用Jetty搭建Java Websocket Server,实现图像传输

    https://my.oschina.net/yushulx/blog/298140 How to Implement a Java WebSocket Server for Image Transm ...