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. 11th day

    今天MySQL数据库的基本知识就学完了,明天开始做小项目什么的,有点小激动啊... <?php // 定义$sql语句执行函数 function my_query($sql){ $result ...

  2. Lucene多字段搜索

    最近在学习Lucene的过程中遇到了需要多域搜索并排序的问题,在网上找了找,资料不是很多,现在都列出来,又需要的可以自己认真看看,都是从其他网站粘贴过来的,所以比较乱,感谢原创的作者们!     使用 ...

  3. MYSQL 体系结构图

  4. [转] Java中的访问控制

    可见/访问性 在同一类中 同一包中 不同包中  同一包子类中  不同包子类中   public  yes  yes  yes  yes  yes  protected   yes  yes  no   ...

  5. QT pro文件解析

    在QT中使用qmake自动生成pro文件,如果要自己定制工程选项,则需要自行修改pro文件. pro文件有以下关键字:TEMPLATE.TARGET.DESTDIR.DEPENDPATH.INCLUD ...

  6. Pyhton开发【第五篇】:Python基础之杂货铺

    Python开发[第五篇]:Python基础之杂货铺   字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进 ...

  7. 登录模块需要用到session留底

    <% HttpSession sessions=request.getSession(); Object sessionValues=sessions.getAttribute("ui ...

  8. asp.net微信开发第五篇----用户分组管理

    上一篇已讲解到新建用户分组,移动用户到分组的功能,这一章主要讲解修改分组名称和删除分组 开发者可以使用接口,对公众平台的分组进行查询.创建.修改.删除等操作,也可以使用接口在需要时移动用户到某个分组. ...

  9. linux下系统定时任务配置----crontab(mysql定时备份)

    crontab命令用于设置周期性被执行的指令,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程,crond进程每分钟会定期检查是否有要执行的任 ...

  10. jsp中文乱码终极解决方法(转)

    一, 找出问题的根源乱码可能出现的地方: jsp页面中 jsp页面之间相互传参的参数 与数据库中数据的存取 基本归纳为以上几种. 二, 寻找解决方案 出现在jsp页面中,是由于没有设置jsp页面的中文 ...