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. AI 人工智能 探索 (七)

    我简单画了一幅图,来讲下 ai 中的设计模式.图形中的这些人物,我想大家都能看的明白. 当 盗贼出现,人们发现了他们,就 呼叫 主类,然后主类再  通知 下面对应的管理局,然后管理局再 分配人手过去 ...

  2. js框架——angular.js(4)

    1. angular中的对象 其实也不用多说的,前台是可以提取后台定义的对象的—— <body ng-app="MyApp"> <div ng-controlle ...

  3. elasticsearch使用jetty进行简单的权限控制

    默认elasticsearch是使用netty作为http的容器,由于netty并没有权限模块,所以默认es没有任何的权限控制,直接通过http就可以进行任何操作,除非把http禁用.但如果你使用el ...

  4. sql 语句操作

    插入:insert into table1(field1,field2) values(value1,value2) db.execSQL(sql) db.execSQL(sql, bindArgs) ...

  5. Java 多态,重载,重写

    1.多态(polymorphism): 多态是指程序中定义的引用变量所指向的具体类型和通过该引用变量发出的方法调用在编程时并不确定,而是在程序运行期间才确定,即一个引用变量倒底会指向哪个类的实例对象, ...

  6. php报错 Call to undefined function mb_stripos()

    错误原因 没有mbstring扩展 本文只介绍Linux解决办法 方法一 编译PHP的时候 带上--enable-mbstring参数 方法二 进入PHP源码/ext/mbstring目录 ./con ...

  7. Linux链接VPN进行转发

    1.安装client sudo apt-get install pptp-linux 2.连接vpn server sudo pptpsetup --create pptpd --server x.x ...

  8. Processes and Threads (转)

    http://www.cnblogs.com/xitang/archive/2011/09/24/2189460.html 原文 http://developer.android.com/guide/ ...

  9. Android平台中实现对XML的三种解析方式

    本文介绍在Android平台中实现对XML的三种解析方式. XML在各种开发中都广泛应用,Android也不例外.作为承载数据的一个重要角色,如何读写XML成为Android开发中一项重要的技能. 在 ...

  10. Zigbee协议栈OSAL层API函数【转载】

              OSAL层提供了很多的API来对整个的协议栈进行管理.主要有下面的几类:信息管理.任务同步.时间管理.中断管理.任务管理.内存管理.电源管理以及非易失存储管理.看到这些管理是不是感 ...