使用游标、存储过程、pivot 三种方法导入数据
--使用游标循环
if (exists (select * from sys.objects where name = 'Base_RecordTend_Test'))
drop proc Base_RecordTend_Test
go
CREATE PROCEDURE Base_RecordTend_Test
AS
declare @ID nvarchar(20),
@Worker nvarchar(50),
@WorkerName nvarchar(50),
@ReceiveType nvarchar(50),
@sql nvarchar(max)
declare mycursor cursor for select top 1 id,Receive_Worker,Receive_WorkerName,Receive_filetype from Archive_Detail_Receive where Receive_filetype is not null
open mycursor
fetch next from mycursor into @ID,@Worker, @WorkerName, @ReceiveType while (@@fetch_status=0)
begin
set @sql =' insert into Base_RecordTend( RT_WorkerNum,RT_workerName,RT_FileId, ['+@ReceiveType+'])values('''+@Worker+''','''+@WorkerName+''','''+@ID+''','''+@ReceiveType+''')'
EXEC sp_executesql @sql
print @sql
fetch next from mycursor into @ID,@Worker, @WorkerName, @ReceiveType
end
close mycursor
DEALLOCATE mycursor
GO exec Base_RecordTend_Test --循环插入数据 if(exists (select * from sys.objects where name='BaseRecordTend_XH'))
drop proc BaseRecordTend_XH
go
create proc BaseRecordTend_XH
as
begin
declare @ID nvarchar(20),
@Worker nvarchar(50),
@WorkerName nvarchar(50),
@ReceiveType nvarchar(50),
@sql nvarchar(max),
@count int,
@number int ; -- select top 1 @count=count(*) from Archive_Detail_Receive where Receive_filetype is not null
set @count=5;
set @number=0; if(@count is not null and @count<>0)
begin
while @number <@count
begin
select @number = @number +1;----每循环一次循环条件+1
select @ID=AR.id,@Worker=AR.Receive_Worker,@WorkerName=AR.Receive_WorkerName,@ReceiveType=AR.Receive_filetype
from(select ROW_NUMBER() over (order by id) 'rowindex',* from Archive_Detail_Receive where Receive_filetype is not null) as AR where AR.rowindex = @number;
set @sql =' insert into Base_RecordTend( RT_WorkerNum,RT_workerName,RT_FileId, ['+@ReceiveType+'])values('''+@Worker+''','''+@WorkerName+''','''+@ID+''','''+@ReceiveType+''')'
EXEC sp_executesql @sql
end end
end
go exec BaseRecordTend_XH --pivot 行转列
select Receive_Worker,Receive_WorkerName,id,
(case when cast( ltrim([1-1]) as int) > 0 then '1-1' when cast(ltrim([1-1]) as int)=0 then null end) as [1-1] ,
(case when cast( ltrim([2-1]) as int) > 0 then '2-1' when cast(ltrim([2-1]) as int)=0 then null end) as [2-1] ,
(case when cast( ltrim([3-1]) as int) > 0 then '3-1' when cast(ltrim([3-1]) as int)=0 then null end) as [3-1] ,
(case when cast( ltrim([4-1-1]) as int) > 0 then '4-1-1' when cast(ltrim([4-1-1]) as int)=0 then null end) as [4-1-1] ,
(case when cast( ltrim([4-2-1]) as int) > 0 then '4-2-1' when cast(ltrim([4-2-1]) as int)=0 then null end) as [4-2-1] ,
(case when cast( ltrim([4-3-1]) as int) > 0 then '4-3-1' when cast(ltrim([4-3-1]) as int)=0 then null end) as [4-3-1] ,
(case when cast( ltrim([4-4-1]) as int) > 0 then '4-4-1' when cast(ltrim([4-4-1]) as int)=0 then null end) as [4-4-1] ,
(case when cast( ltrim([5-1]) as int) > 0 then '5-1' when cast(ltrim([5-1]) as int)=0 then null end) as [5-1] ,
(case when cast( ltrim([6-1]) as int) > 0 then '6-1' when cast(ltrim([6-1]) as int)=0 then null end) as [6-1] ,
(case when cast( ltrim([7-1]) as int) > 0 then '7-1' when cast(ltrim([7-1]) as int)=0 then null end) as [7-1] ,
(case when cast( ltrim([8-1]) as int) > 0 then '8-1' when cast(ltrim([8-1]) as int)=0 then null end) as [8-1] ,
(case when cast( ltrim([9-1-1]) as int) > 0 then '9-1-1' when cast(ltrim([9-1-1]) as int)=0 then null end) as [9-1-1] ,
(case when cast( ltrim([9-2-1]) as int) > 0 then '9-2-1' when cast(ltrim([9-2-1]) as int)=0 then null end) as [9-2-1] ,
(case when cast( ltrim([9-3-1]) as int) > 0 then '9-3-1' when cast(ltrim([9-3-1]) as int)=0 then null end) as [9-3-1] ,
(case when cast( ltrim([10-1]) as int) > 0 then '10-1' when cast(ltrim([10-1]) as int)=0 then null end) as [10-1]
from
( select top 100 id,Receive_Worker,Receive_WorkerName,Receive_filetype from Archive_Detail_Receive )as tend
PIVOT
( COUNT( Receive_filetype )
for Receive_filetype in ([1-1], [2-1], [3-1],[4-1-1],[4-2-1],[4-3-1],[4-4-1],[5-1],[6-1],[7-1],[8-1],[9-1-1],[9-2-1],[9-3-1],[10-1] )) as a
使用游标、存储过程、pivot 三种方法导入数据的更多相关文章
- SQL Server 优化存储过程的七种方法
原文:SQL Server 优化存储过程的七种方法 优化存储过程有很多种方法,下面介绍最常用的7种. 1.使用SET NOCOUNT ON选项 我们使用SELECT语句时,除了返回对应的结果集外,还会 ...
- 服务器文档下载zip格式 SQL Server SQL分页查询 C#过滤html标签 EF 延时加载与死锁 在JS方法中返回多个值的三种方法(转载) IEnumerable,ICollection,IList接口问题 不吹不擂,你想要的Python面试都在这里了【315+道题】 基于mvc三层架构和ajax技术实现最简单的文件上传 事件管理
服务器文档下载zip格式 刚好这次项目中遇到了这个东西,就来弄一下,挺简单的,但是前台调用的时候弄错了,浪费了大半天的时间,本人也是菜鸟一枚.开始吧.(MVC的) @using Rattan.Co ...
- C# | VS2019连接MySQL的三种方法以及使用MySQL数据库教程
本文将介绍3种添加MySQL引用的方法,以及连接MySQL和使用MySQL的教程 前篇:Visual Studio 2019连接MySQL数据库详细教程 \[QAQ \] 第一种方法 下载 Mysql ...
- 获得Window窗口权限的三种方法
1.第一种方法:利用视图控制器自带的View的window属性: 具体使用 self.view.window.rootViewController = ... 2.第二种方法:通过导入APPDele ...
- 【Cocos2d-x游戏开发】解决Cocos2d-x中文乱码的三种方法
众所周知,Cocos2d-x是一款不错的开源引擎,但是在Cocos2d-x中直接使用中文是无法正确显示的.比如下面的情况: 解决这个问题常用的有三种方法:1.通过转换为UTF-8编码来显示.2.使用i ...
- Objective-C:三种文件导入的方式以及atomic和nonatomic的区别
一.三种文件导入的方式比较: 类的前项声明@class.import.include: 1.采用@class 类名的方式,它会告诉编译器有这么一个类,目前不需要知道它内部的实例变量和方法是如何定义 ...
- Centos内核升级的三种方法
本文出自 “存储之厨” 博客,请务必保留此出处http://xiamachao.blog.51cto.com/10580956/1755354 在基于CentOS平台的工作过程中,难免有时需要升级或者 ...
- 纯Css绘制三角形箭头三种方法
在制作网页的过程中少不了绘制类似图片的三角形箭头效果,虽然工程量不大,但是确实麻烦.在学习的过程中,总结了以下三种方法,以及相关的例子. 一.三种绘制三角形箭头方法 1.方法一:利用overflow: ...
- OC和JS交互的三种方法
看简书上说一共有六种OC和JS交互的方法,但是前三种原理都一致,都是通过检测.拦截Url地址实现互相调用的.剩下的react native等第三方框架原理不一样,也没有去研究,下边记录我使用的三种方法 ...
随机推荐
- Pyspark访问Hbase
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl/p/7347167.html 转载请注明出处 记录自己最近抽空折腾虚拟机环境时用spark2.0的pyspark ...
- FFmpeg从入门到精通——进阶篇,SEI那些事儿
前言 在直播应用的开发过程中,如果把主播端消息事件传递到观众端,一般会以Instant Messaging(即时通讯)的方式传递过去,但因为消息分发通道和直播通道是分开的,因此消息与直播音视频数据的同 ...
- CF1155D Beautiful Array(动态规划)
做法 \(f_{i,0}\)表示以\(i\)结尾未操作时的最大值 \(f_{i,1}\)表示以\(i\)结尾正在操作时的最大值 \(f_{i,2}\)表示以\(i\)结尾已结束操作时的最大值 Code ...
- JavaScript常用工具方法封装
因为工作中经常用到这些方法,所有便把这些方法进行了总结. JavaScript 1. type 类型判断 isString (o) { //是否字符串 return Object.prototype. ...
- mysql搜索不区分大小写
mysql搜索是不区分大小写的,这种情况下我们有两种方法解决 知识前提: BINARY binary不是函数,而是一个类型转换运算符,它用来强制字符串为一个二进制字符串,可以理解为在字符串比较的时候区 ...
- Bag类的接口的实现与测试(课上测试补做)
Bag类的接口的实现与测试(课上测试补做) 截图 由于截图有一定的的限制就没有吧所有的代码截进去,后面有代码. 代码 package ClassTest; import java.util.Objec ...
- [BZOJ1584]Cleaning Up 打扫卫生
Description 有N头奶牛,每头那牛都有一个标号Pi,1 <= Pi <= M <= N <= 40000.现在Farmer John要把这些奶牛分成若干段,定义每段的 ...
- db2快照
一.获取快照日志 #1.查看数据库编目 db2 list db directory #2.attach 到要分析的数据库 db2 attach to pm1_9 user db2dev #3.conn ...
- shell 条件语句
shell 条件语句 #!/bin/bash # 条件语句 NUM1=100 NUM2=200 if (($NUM1 > $NUM2));then echo "$NUM1 greate ...
- Swoft 快速上手小贴士
IDE一定要装注解插件PHP Annotations Request和Response里的with...开头的方法会clone $this, 而不是修改本实体, 所以设置Cookie之类的时候要$re ...