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 ...
随机推荐
- AI 人工智能 探索 (九)
链接:http://pan.baidu.com/s/1c0AM3g0 密码:uccw 今天补充 创建物体 移动物体 ,当点击创建后 ,会出来一个 上图的 ui,他跟随 物体,当你把物体拖动到 指定的地 ...
- 函数FindFirstFile
文件查找 Struct Lnode{ Char table[256]; //保存文件夹名 Struct Lnode*next; } 加文件夹入链表 Lnode *newList; Vo ...
- HDU1372:Knight Moves(BFS)
Knight Moves Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- Extjs视频
Extjs视频http://www.youku.com/playlist_show/id_19343298.html ExtJs视频教程(关东升) 智捷关东升老师ExtJs视频教程AJAX框架-Ext ...
- ActiveXObject函数详解(转)
什么是 ActiveX 控件? ActiveX 控件广泛用于 Internet.它们可以通过提供视频.动画内容等来增加浏览的乐趣.不过,这些程序可能出问题或者向您提供不需要的内容.在某些情况下,这些程 ...
- 转 Android:sp与dp(densityDpi与scaledDensity)
一般在布局上设置控件大小维度的单位采用dp,而定义字体大小的单位采用sp. dp是dip,density independent pixel,即密度无关的像素单位,说白了,就是这个维度相对于不同屏幕的 ...
- 加密Web.Config配置文件
protected void btnPassWord_Click(object sender, EventArgs e) { //加密 Configuration config = WebConfig ...
- 关于oracle 还原数据库的要领
create tablespace DSXZFW datafile 'D:\yangk\oraclespace\DSXZFW.ora' size 1000m; // 创建表空间,注意如果要还原数据库的 ...
- initWithNibName与viewDidLoad的执行关系以及顺序
转载自:http://www.java123.net/v/380331.html 一个ViewController,一般通过init或initWithNibName来加载.二者没有什么不同,init最 ...
- 最短路 HDU 2544
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...