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 ...
随机推荐
- 命令行调试smali
am start -D -n com.agilebits.onepassword/.activity.LoginActivity ps | grep passu0_a126 1107 217 5107 ...
- gulp相关知识(2)
将之前的东西的理论部分整理了一下—— gulp 前端构建工具 它可以帮助我们完成一些自动化操作 它需要一些插件的支持 var gulp = require('gulp'); --> gulp命令 ...
- ntfs mount fail after upgrade win10
http://www.cnblogs.com/wangbo2008/p/3782730.html linux下挂载NTFS分区错误修复 今天在linux下打开win的NTFS硬盘总是提示出错了,而 ...
- PKI 笔记
PKI – Public Key Infrastructure , 通常翻译为公钥基础设施. PKI 安全平台提供的4个服务,来保证安全的数据,分别是: l 身份识别 l 数据保密 l 数据完整 ...
- mr本地运行的几种模式
MR程序的几种提交运行模式 本地模型运行 1/在windows的eclipse里面直接运行main方法,就会将job提交给本地执行器localjobrunner执行 ----输入输出数据可以放在本地路 ...
- 转:WebTest的常见问题与解决
WebTest的常见问题与解决录制好一个WebTest,加上各种规则,编辑后运行并不会像我们想象的那么顺利成功,往往会碰到很多问题,运行不成功的情况比较多,这样我们就遇到了如何解决这些问题的情形.1. ...
- 防止aspx木马的IIS SPY变态功能
防止aspx木马的IIS SPY变态功能 如果服务器支持aspx语言,而且被上传了aspx木马,利用木马里面的IIS SPY 功能,可以读出IIS里面的所有用户的密码,包括用IIS做FTP的,也能读出 ...
- [iOS开发]WKWebView加载JS
最近项目要用webView加载js文件,挺同事说WKWebView比UIWebView更加好用,于是我今天就试试,百度一发,自己写了个demo. 先看我写的代码,然后再来看WKWebView跟UIWe ...
- Android Studio的使用(三)--包不分级、修改包名
1.如果不喜欢将包逐级展开的话,可以将每一个包名都完整展现出来,只需要勾选Flatten Packages. 2.修改包名 3.填写新的包名 4.点击确认
- JavaScript 中实现继承的方式(列举3种在前一章,我们曾经讲解过创建类的最好方式是用构造函数定义属性,用原型定义方法。)
第一种:对象冒充 function ClassA(sColor) { this.color = sColor; this.sayColor = function () { alert(this.col ...