sqlserver 脚本方式导出数据到excel
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的更多相关文章
- 使用python脚本从数据库导出数据到excel
python从数据库导出数据到excel 最近需要从数据库里导出一些数据到excel,刚开始我是使用下面的命令 select * from xxx where xxx into outfile 'xx ...
- 使用Open xml 操作Excel系列之二--从data table导出数据到Excel
由于Excel中提供了透视表PivotTable,许多项目都使用它来作为数据分析报表. 在有些情况下,我们需要在Excel中设计好模板,包括数据源表,透视表等, 当数据导入到数据源表时,自动更新透视表 ...
- ASP.NET导出数据到Excel 实例介绍
ASP.NET导出数据到Excel 该方法只是把asp.net页面保存成html页面只是把后缀改为xlc不过excel可以读取,接下连我看看还有别的方式能导出数据,并利用模版生成. 下面是代码 新建 ...
- python 导出数据到excel 中,一个好用的导出数据到excel模块,XlsxWriter
最近公司有项目需要导出数据到excel,首先想到了,tablib,xlwt,xlrd,xlwings,win32com[还可以操作word],openpyxl,等模块但是 实际操作中tablib 写入 ...
- Delphi 导出数据至Excel的7种方法【转】
一; delphi 快速导出excel uses ComObj,clipbrd; function ToExcel(sfilename:string; ADOQuery:TADOQuery): ...
- 微软BI 之SSIS 系列 - 导出数据到 Excel 2013 的实现
开篇介绍 碰到有几个朋友问到这个问题,比较共性,就特意写了这篇小文章说明一下如何实现在 SSIS 中导出数据到 Office Excel 2013 中.通常情况下 2013 以前的版本大多没有问题,但 ...
- phpexcel如何读取excel的数据和如何导出数据到excel
phpexcel如何读取excel的数据和如何导出数据到excel 一.总结 一句话总结:去官网看参考手册和api,或者找中文的博客或者参考手册 1.phpexcel插件如何下载? 其实这些插件不仅可 ...
- 从DataTable高效率导出数据到Excel
首先从数据库读取数据到DataTable,这我就不提了,大家都明白.下面直接介绍如何从DataTable高效率导出数据到Excel中的方法,代码如下: using Microsoft.Office.I ...
- Delphi 导出数据至Excel的7种方法
一; delphi 快速导出excel uses ComObj,clipbrd; function ToExcel(sfilename:string; ADOQuery:TADOQuery):bool ...
随机推荐
- js中对象相关
对象的创建: var first=new Object(); var second={ a:"bbb", b:function(){...} } 对象的内容增加: first.na ...
- HDU 2209 翻纸牌游戏
翻纸牌游戏 Time Limit : 9000/3000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submiss ...
- elasticsearch集群内部节点超时解决
默认配置为:节点每隔1s同master发送1次心跳,超时时间为30s,测试次数为3次,超过3次,则认为该节点同master已经脱离了.以上为elasticsearch的默认配置.在实际生产环境中,每隔 ...
- Linux -- ls只显示目录
ls没有直接显示目录的选项, 不过根据目录和文件显示的差异,可以搭配grep来实现 方法1: ll | grep "^d" 方法2: ls -F | grep$ "/$& ...
- asp 特殊字符替换
<%Function specialstr(yourstring)find= "¿,À,Á,Â,Ã,Ä,Å,Æ,Ç,È,É,Ê,Ë,Ì,Í,Î,Ï,Ð,Ñ,Ò,Ó,Ô,Õ,Ö,Ø,Ù, ...
- FragmentTabHost使用注意
FragmentTabHost使用时每次切换回Fragment时,都会再走一遍onCreateView,解决办法是缓存View,具体如下 private View rootView;//缓存Fragm ...
- 监控redis进程,如果没有自动重启
监控redis进程,如果没有自动重启 #Time:2016-01-22#Version:1.0 #Author:chh-huang #设置环境变量source /etc/profile#source ...
- 正则抓取网页所有href和src
根据抓取的页面,用正则来匹配页面href和src string UserAgent = "Mozilla/5.0 (Windows NT 5.2; rv:29.0) Gecko/201001 ...
- UIView 视图切换
UIView之间常用视图之间切换方式 转载自:http://www.jianshu.com/p/0d53f9402c07 在平时编写代码的过程中,页面之间的跳转可以说就和MVC模式一样是开发必须的.但 ...
- [转]makefile文件的编写规则及实例
http://xueqi.iteye.com/blog/1567866 1.一个简单的makefile例子 假设一个程序有两个文件file1.c,file2.c,每个文件都包含head.h,生 ...