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数据导入、跨服务器数据拷贝、行转列示例的更多相关文章

  1. node开发后将本地mysql数据导入到服务器mysql

    近期写的一个钉钉企业微应用用到了mysql数据库(用koa写的后台,并用mysql库来连接),现在需要把本地数据库的数据导入到服务器的数据库中. 服务器安装mysql 可以google篇centos的 ...

  2. 阶段3 3.SpringMVC·_05.文件上传_5 文件上传之跨服务器上传分析和搭建环境

    使用这个jar包来跨服务器上传 搞两个tomcat.一个springmvc一个fileupload 选中tomcat server点击左边的加号 需要改端口和JMX pport这个端口 部署文件上传的 ...

  3. pl/sql 如何将Excel文件数据导入oracle的数据表?

    1.准备导入数据的excel文件 注意:excel列名和数据表列名必须相同,excel文件sheet2和sheet3可以删除 1)excel文件格式 2)数据表格式 2.打开pl/sql ,找到工具- ...

  4. 阶段3 3.SpringMVC·_05.文件上传_6 文件上传之跨服务器上传代码

    参数HttpServletRequest也可以删掉  扩服务器的代码 注意选择jersey包下的 拿到文件资源.put过去. 最终代码 重新部署springMvc 图片服务器正常运行 客户端服务器 服 ...

  5. ABAP-1-会计凭证批量数据导入本地ACCESS

    公司会计凭证导入ACCESS数据库,需要发送给审计,原先的方案是采用DEPHI开发的功能(调用函数获取会计凭证信息,然后INSERT到ACCESS数据表),运行速度非常慢,业务方要求对该功能进行优化, ...

  6. 效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】) 转

    效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中[附源代码下载])    本文目录: (一)背景 (二)数据库数据导入到Excel的方法比较   ...

  7. Visual Basic 2012 借助DataGridView控件将Excel 2010数据导入到SQL server 2012

    (注:注释的颜色原本为绿色,在这里变为黑色,有点不便,但不会造成阅读影响.放入Visual Basic2012代码编辑器后会还原成绿色.) 摘  要:DataGridView控件作为数据传输的中介,只 ...

  8. 解决将Excel表导入到SQL Server数据库时出现Text was truncated or one or more characters had no match in the target code错误

    编写python爬虫程序可以在电商.旅游等网站上爬取相关评论数据,这些数据可以用于词云制作.感情词分析.提取关键词等,也可以将爬取下来的数据以自己的方式进行展示.评论数据爬取下来后,就要考虑怎样入库, ...

  9. Java实现FTP跨服务器文件操作

    在过去的几年工作中,曾经多次需要把文件上传到单独的服务器,而程序是在单独的服务器上部署的,在进行文件操作的时候就需要跨服务器进行操作包括:文件上传.文件下载.文件删除等.跨服务器文件操作一般是需要FT ...

随机推荐

  1. README 语法编写

    推荐一个超棒的软件 haroopad Standard Markdown \ backslash ` backtick * asterisk _ underscore {} curly braces ...

  2. ionic之应用首次启动引导页

    用户首次启动app先进入引导页,localstroge记录状态,下次启动应用不再显示引导页. HTML: <html> <head> <meta charset=&quo ...

  3. zookeeper选举代码分析

    本文将以zookeeper的3.4.6版本作为源码分析版本.主要的代码类包括QuorumPeerMain.QuorumPeer.FastLeaderElection.QuorumMaj等. 假设有a, ...

  4. xcode设置项目图标玻璃镜效果

    xcode5中设置 ios6和ios7的适配一些小细节注意,ios6中图标会默认的设置玻璃镜效果 找到图片文件夹APPlcon中右侧设置中的有个iOS icon is pre-rend-rendere ...

  5. Queueing in the Linux Network Stack !!!!!!!!!!!!!!!

    https://www.coverfire.com/articles/queueing-in-the-linux-network-stack/ Queueing in the Linux Networ ...

  6. NSDateFormatter 格式说明

    格式化参数如下:    G: 公元时代,例如AD公元    yy: 年的后2位    yyyy: 完整年    MM: 月,显示为1-12    MMM: 月,显示为英文月份简写,如 Jan    M ...

  7. 站内信,群发与全部发送。Gson解析result

    /** * 发送站内信 */@Permission(Module.TZGL)@RequestMapping(value = "/sendznx", method = Request ...

  8. python 之路,Day11 (下)- sqlalchemy ORM

    python 之路,Day11 - sqlalchemy ORM   本节内容 ORM介绍 sqlalchemy安装 sqlalchemy基本使用 多外键关联 多对多关系 表结构设计作业 1. ORM ...

  9. python学习之-成员信息增删改查

    python学习之-成员信息增删改查 主要实现了成员信息的增加,修改,查询,和删除功能,写着玩玩,在写的过程中,遇到的问题,旧新成员信息数据的合并,手机号和邮箱的验证,#!/usr/bin/env p ...

  10. Android客户端与服务端交互之登陆示例

    Android客户端与服务端交互之登陆示例 今天了解了一下android客户端与服务端是怎样交互的,发现其实跟web有点类似吧,然后网上找了大神的登陆示例,是基于IntentService的 1.后台 ...