游标、获取本地本地多个文件、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 ...
随机推荐
- TextView的ems和MaxLength
<EditText android:id="@+id/et_code" android:layout_width="wrap_content" andro ...
- 【转】Android Studio中Git的配置及协同开发
一. Android Stutio配置git setting–>Version Control–>Git–>Path to Git executable中选择git.exe的位置 ...
- Android--图片的三级缓存策略
三级缓存缓存(内存)--->本地磁盘---->网络1.首先看一下图片存储到本地磁盘 public class FileUtils { String path;//文件存储的地方 publi ...
- OpenGL蓝宝书第五章代码勘误以及惯性坐标系去解释模型变换:Pyramid.cpp
假设你也发现依照教程代码完毕贴图时,你会底面的坐标和寻常顶点坐标正负相反,比方-1.0f, -1.0f, -1.0f这个顶点相应的却是世界坐标中1.0f,-1.0f,1.0f 问题到底出如今哪里? 原 ...
- C++ 让 Win32 Console Application 程序后台运行
方法一:(无闪现) 添加 #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRT ...
- 自定义filter包
在有些时候,你可能需要以你的所有项目进行全局的过滤. 因为你的项目可以设计到互相的依赖和调用 . 修改在tomcat下的conf下的web.xml文件.和在原来的web-inif下的修改一样,添加fi ...
- Android音频开发之——如何播放一帧音频
本文重点关注如何在Android平台上播放一帧音频数据.阅读本文之前,建议先读一下<Android音频开发(1):基础知识>,因为音频开发过程中,经常要涉及到这些基础知识,掌握了这些重要的 ...
- 后台地址报错:Service Unavailable
首先考虑数据库是否打开? 第二重启IIS试试: 重启下iis试试(cmd接着iisreset) 再次访问就正常了,可以借鉴,但不一定就只是这一种原因.
- 关于iframe调用父页面元素操作
在iframe子页面获取父页面元素 代码如下: //在iframe子页面获取父页面元素 $.('#objld', parent.document); //在父页面获取iframe子页面的元素 $(&q ...
- Ajax请求传递参数遇到的问题
想写个同类型的,代码未测. 什么是WebAPI?我的理解是WebAPI+JQuery(前端)基本上能完成Web MVC的功能,即:这么理解吧,WebAPI相当于Web MVC的后台部分. 接下来直接上 ...