游标、获取本地本地多个文件、Excel数据导入、跨服务器数据拷贝、行转列示例
declare @dirPath varchar(200)='C:\Users\Administrator\Desktop\待处理数据\顺江学校4\' --------------------------------------------------------------------------------------------获取本地文件夹下多个文件----------------------------------------------------------
if(OBJECT_ID('ff')>0)
drop table ff create table ff
(
id int identity(1,1),
fName varchar(300),
depth int,
isFile int
) insert into ff
exec xp_dirtree @dirPath, 0, 1 --select * from ff if(OBJECT_ID('RawScore')>0)
drop table RawScore CREATE TABLE [dbo].[RawScore](
[F] [varchar](50) NULL,
[F0] [varchar](50) NULL,
[F1] [nvarchar](255) NULL,
[F2] [nvarchar](255) NULL,
[F3] [float] NULL,
[F4] [float] NULL,
[F5] [float] NULL,
[F6] [nvarchar](255) NULL,
[F7] [float] NULL,
[F8] [float] NULL,
[F9] [float] NULL
) declare @studentId varchar(50)='',@studentName varchar(50)='',@studentInfo varchar(100)='';
declare @pos int=0,@len int =0;
declare @fileName varchar(100);
declare @sql varchar(max) = ''; ------------------------------------------------------------------游标操作-----------------------------------------------------------------------------
declare cur cursor for
select fName from ff open cur
fetch next from cur into @fileName while @@FETCH_STATUS=0
begin set @studentInfo=SUBSTRING(@fileName,1,patindex('%.xls',@fileName)-1)
set @pos = PATINDEX('%[_]%',@studentInfo);
set @len = LEN(@studentInfo);
set @studentName = SUBSTRING(@studentInfo,1,@pos-1);
set @studentId = SUBSTRING(@studentInfo,@pos+1,@len);
--select @studentName,@studentId --------------------------------------------------------------------------------------导入本地Excel文件数据---------------------------------------------------------------------------
set @sql = 'insert into RawScore
select '''+@studentId+''','''+@studentName+''',* from OPENROWSET(''Microsoft.Jet.OLEDB.4.0'',''Excel 8.0;Database='+@dirPath+@fileName+''', [Sheet1$])';
exec(@sql) fetch next from cur into @fileName end close cur
deallocate cur if(OBJECT_ID('StudentScore')>0)
drop table StudentScore CREATE TABLE [dbo].[StudentScore](
[Id] [uniqueidentifier] NOT NULL,
[StudentID] [varchar](50) NULL,
[Name] [varchar](50) NULL,
[Kind] [int] NULL,
[ItemName] [varchar](50) NULL,
[Score] [float] NULL
) insert into studentScore
select NEWID(), f,F0,1,F2,f5 from RawScore where f2!='内容板块(满分)' and PATINDEX('%[(]%',F2)>0
union all
select NEWID(), f,F0,2,F6,f9 from RawScore where f6!='能力层次(满分)' and PATINDEX('%[(]%',F6)>0 select * from StudentScore order by name -------------------------------------------------------跨服务器链接数据库进行数据操作-------------------------------------------------
--declare @count int=0 --select @count=COUNT(*) from sys.servers where name='syncDBLink'
--if(@count > 0)
--begin
-- exec sp_dropserver 'syncDBLink','droplogins'
--end ----打开指定服务器上的数据库
--exec sp_addlinkedserver 'syncDBLink','','SQLOLEDB','192.168.0.102','','','wangyue0428';
--exec sp_addlinkedsrvlogin 'syncDBLink',false,null,'sa','HX1q2w3e4r';
--exec sp_serveroption 'syncDBLink','rpc out','true'; --delete from syncDBLink.wangyue0428.dbo.StudentScore --insert into syncDBLink.wangyue0428.dbo.StudentScore
--select * from StudentScore --exec sp_dropserver 'syncDBLink','droplogins' --select StudentID,Name,Kind,SUM(Score) from StudentScore
--group by StudentID,Name,Kind --select distinct studentId,name from StudentScore -----------------------------------------------------------------------------------行转列实现-------------------------------------------------------------------
;with cte1 as
(
select StudentID,Name, [拼音练习(8.00)],[字词练习(16.00)],[句子练习(21.00)],[课内文段阅读(12.00)],[课外文段阅读(18.00)],[习作(25.00)]
,[拼音练习(8.00)]+[字词练习(16.00)]+[句子练习(21.00)]+[课内文段阅读(12.00)]+[课外文段阅读(18.00)]+[习作(25.00)] as 小计
from
(
select StudentID,Name,ItemName,Score from syncDBLink.wangyue0428.dbo.StudentScore where Kind=1
) as s
pivot
(
sum(Score)
for ItemName in([拼音练习(8.00)],[字词练习(16.00)],[句子练习(21.00)],[课内文段阅读(12.00)],[课外文段阅读(18.00)],[习作(25.00)])
) as pv
)
,cte2
as
(
select StudentID,Name, [识记(18.00)],[表达应用(51.00)],[理解(16.00)],[分析综合(15.00)]
,[识记(18.00)]+[表达应用(51.00)]+[理解(16.00)]+[分析综合(15.00)] as 小计
from
(
select StudentID,Name,ItemName,Score from syncDBLink.wangyue0428.dbo.StudentScore where Kind=2
) as s
pivot
(
sum(Score)
for ItemName in([识记(18.00)],[表达应用(51.00)],[理解(16.00)],[分析综合(15.00)])
) as pv
)
select ROW_NUMBER() over(order by cte1.小计 desc) as 序号, '顺江中学' as 学校名称,cte1.Name as 姓名,'' as 性别,[拼音练习(8.00)],[字词练习(16.00)],[句子练习(21.00)],[课内文段阅读(12.00)],[课外文段阅读(18.00)],[习作(25.00)],cte1.小计
,[识记(18.00)],[表达应用(51.00)],[理解(16.00)],[分析综合(15.00)],cte2.小计
from cte1
inner join cte2 on cte1.StudentID=cte2.StudentID
游标、获取本地本地多个文件、Excel数据导入、跨服务器数据拷贝、行转列示例的更多相关文章
- node开发后将本地mysql数据导入到服务器mysql
近期写的一个钉钉企业微应用用到了mysql数据库(用koa写的后台,并用mysql库来连接),现在需要把本地数据库的数据导入到服务器的数据库中. 服务器安装mysql 可以google篇centos的 ...
- 阶段3 3.SpringMVC·_05.文件上传_5 文件上传之跨服务器上传分析和搭建环境
使用这个jar包来跨服务器上传 搞两个tomcat.一个springmvc一个fileupload 选中tomcat server点击左边的加号 需要改端口和JMX pport这个端口 部署文件上传的 ...
- pl/sql 如何将Excel文件数据导入oracle的数据表?
1.准备导入数据的excel文件 注意:excel列名和数据表列名必须相同,excel文件sheet2和sheet3可以删除 1)excel文件格式 2)数据表格式 2.打开pl/sql ,找到工具- ...
- 阶段3 3.SpringMVC·_05.文件上传_6 文件上传之跨服务器上传代码
参数HttpServletRequest也可以删掉 扩服务器的代码 注意选择jersey包下的 拿到文件资源.put过去. 最终代码 重新部署springMvc 图片服务器正常运行 客户端服务器 服 ...
- ABAP-1-会计凭证批量数据导入本地ACCESS
公司会计凭证导入ACCESS数据库,需要发送给审计,原先的方案是采用DEPHI开发的功能(调用函数获取会计凭证信息,然后INSERT到ACCESS数据表),运行速度非常慢,业务方要求对该功能进行优化, ...
- 效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】) 转
效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中[附源代码下载]) 本文目录: (一)背景 (二)数据库数据导入到Excel的方法比较 ...
- Visual Basic 2012 借助DataGridView控件将Excel 2010数据导入到SQL server 2012
(注:注释的颜色原本为绿色,在这里变为黑色,有点不便,但不会造成阅读影响.放入Visual Basic2012代码编辑器后会还原成绿色.) 摘 要:DataGridView控件作为数据传输的中介,只 ...
- 解决将Excel表导入到SQL Server数据库时出现Text was truncated or one or more characters had no match in the target code错误
编写python爬虫程序可以在电商.旅游等网站上爬取相关评论数据,这些数据可以用于词云制作.感情词分析.提取关键词等,也可以将爬取下来的数据以自己的方式进行展示.评论数据爬取下来后,就要考虑怎样入库, ...
- Java实现FTP跨服务器文件操作
在过去的几年工作中,曾经多次需要把文件上传到单独的服务器,而程序是在单独的服务器上部署的,在进行文件操作的时候就需要跨服务器进行操作包括:文件上传.文件下载.文件删除等.跨服务器文件操作一般是需要FT ...
随机推荐
- Atom 编辑器 前端基本插件
Atom 编辑器插件 这个编辑器是github出品,现在处于免费试用期:如果是初学者,可以使用这个编辑器,插件安装很方便,只需要点菜单栏的File-Settings-Install,在搜索框中输入想要 ...
- js~fancybox为我们提供的iframe功能
对于fancybox我们已经耳熟能详了,一般用来到表单的弹框,提示弹框等,而今天,我们将分页列表也使用fancybox来实现一下,这东西听起来简单,但做起来还真不是那么回事,有事细节需要我们注意的,首 ...
- nodejs这个过程POST求
下面是一个web登陆模拟过程.当我们问一个链接,你得到一个表格,然后填写相应的表格值,然后提交登陆. var http = require('http'); var querystring = req ...
- XTU OJ 1210 Happy Number (暴力+打表)
Problem Description Recently, Mr. Xie learn the concept of happy number. A happy number is a number ...
- TCP的封包与拆包
对于基于TCP开发的通讯程序,有个很重要的问题需要解决,就是封包和拆包. 一.为什么基于TCP的通讯程序需要进行封包和拆包. TCP是个"流"协议,所谓流,就是没有界限的一串数据. ...
- C#中的IO流操作(FileStream)
StreamReader和StreamWriter适用于对文本文件的操作,因为它是以字符为单位进行的操作 不用担心编码问题 using (Stream s = new FileStream(@&quo ...
- C#word(2007)操作类--新建文档、添加页眉页脚、设置格式、添加文本和超链接、添加图片、表格处理、文档格式转化
转:http://www.cnblogs.com/lantionzy/archive/2009/10/23/1588511.html 1.新建Word文档 #region 新建Word文档/// &l ...
- C#基础知识01(continue、break 和 return、ref 和 out)
break[跳出循环或者退出一个switch语句]由于它是用来退出循环或者switch语句的,所以只有当它出现在这些语句中时才是合法的. continue 语句和break语句相似,只是它不是退出一个 ...
- Objective-C中的内存管理——手动内存管理
一.引用计数(retainCount) 1.内存的申请 alloc和new是Objective-C语言中申请内存的两种常用方式. 当我们向一个类对象发送一个alloc消息的时候,其会向操作系统在堆中申 ...
- ajax省市线三级联动
<script type='text/javascript' src='http://ajax.useso.com/ajax/libs/jquery/1.7.2/jquery.min.js?ve ...