--使用游标循环
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 三种方法导入数据的更多相关文章

  1. SQL Server 优化存储过程的七种方法

    原文:SQL Server 优化存储过程的七种方法 优化存储过程有很多种方法,下面介绍最常用的7种. 1.使用SET NOCOUNT ON选项 我们使用SELECT语句时,除了返回对应的结果集外,还会 ...

  2. 服务器文档下载zip格式 SQL Server SQL分页查询 C#过滤html标签 EF 延时加载与死锁 在JS方法中返回多个值的三种方法(转载) IEnumerable,ICollection,IList接口问题 不吹不擂,你想要的Python面试都在这里了【315+道题】 基于mvc三层架构和ajax技术实现最简单的文件上传 事件管理

    服务器文档下载zip格式   刚好这次项目中遇到了这个东西,就来弄一下,挺简单的,但是前台调用的时候弄错了,浪费了大半天的时间,本人也是菜鸟一枚.开始吧.(MVC的) @using Rattan.Co ...

  3. C# | VS2019连接MySQL的三种方法以及使用MySQL数据库教程

    本文将介绍3种添加MySQL引用的方法,以及连接MySQL和使用MySQL的教程 前篇:Visual Studio 2019连接MySQL数据库详细教程 \[QAQ \] 第一种方法 下载 Mysql ...

  4. 获得Window窗口权限的三种方法

    1.第一种方法:利用视图控制器自带的View的window属性:  具体使用 self.view.window.rootViewController = ... 2.第二种方法:通过导入APPDele ...

  5. 【Cocos2d-x游戏开发】解决Cocos2d-x中文乱码的三种方法

    众所周知,Cocos2d-x是一款不错的开源引擎,但是在Cocos2d-x中直接使用中文是无法正确显示的.比如下面的情况: 解决这个问题常用的有三种方法:1.通过转换为UTF-8编码来显示.2.使用i ...

  6. Objective-C:三种文件导入的方式以及atomic和nonatomic的区别

    一.三种文件导入的方式比较:   类的前项声明@class.import.include: 1.采用@class 类名的方式,它会告诉编译器有这么一个类,目前不需要知道它内部的实例变量和方法是如何定义 ...

  7. Centos内核升级的三种方法

    本文出自 “存储之厨” 博客,请务必保留此出处http://xiamachao.blog.51cto.com/10580956/1755354 在基于CentOS平台的工作过程中,难免有时需要升级或者 ...

  8. 纯Css绘制三角形箭头三种方法

    在制作网页的过程中少不了绘制类似图片的三角形箭头效果,虽然工程量不大,但是确实麻烦.在学习的过程中,总结了以下三种方法,以及相关的例子. 一.三种绘制三角形箭头方法 1.方法一:利用overflow: ...

  9. OC和JS交互的三种方法

    看简书上说一共有六种OC和JS交互的方法,但是前三种原理都一致,都是通过检测.拦截Url地址实现互相调用的.剩下的react native等第三方框架原理不一样,也没有去研究,下边记录我使用的三种方法 ...

随机推荐

  1. List泛型集合转DataTable

    自存,此方法可以防止出现DataSet不支持System.Nullable的错误 public static DataTable ToDataTable<T>(IEnumerable< ...

  2. Spring AOP (事务管理)

    一.声明式事务管理的概括 声明式事务(declarative transaction management)是Spring提供的对程序事务管理的方式之一. Spring的声明式事务顾名思义就是采用声明 ...

  3. mysql更新子查询中的内容

    UPDATE t_finance_certify_copy c SET c.biz_type=2,c.sub_biz_type=18WHERE c.finance_certify_id IN(SELE ...

  4. Netty资料

    netty 资料  转自   http://calvin1978.blogcn.com/articles/netty-info.html Netty资料皆阵列在前 Posted on 2016-08- ...

  5. NIO 01 (转)

    本文转自:http://www.cnblogs.com/littlehann/p/3720396.html 目录 1. NIO.NIO.2简介 2. NIO中的关键技术 1. NIO.NIO.2简介 ...

  6. hadoop2对应的eclipse插件使用

    1. eclipse插件安装步骤: a).把插件复制到eclipse安装目录plugins文件夹下 b).打开eclipse的Window---Preference---Hadoop M/R---指向 ...

  7. ubuntu下源码安装wget

    1.背景 ubuntu18.04 64bit 2.安装方法如下: 2.1.获取源码 curl -o wget-1.20.tar.gz ftp://ftp.gnu.org/gnu/wget/wget-1 ...

  8. iOS开发中的地图开发

    显示地图: 1.导入头文件 #import <MapKit/MapKit.h> 如果同时需要用户定位的话还需要 #import <CoreLocation/CoreLocation. ...

  9. bzoj1879: [Sdoi2009]Bill的挑战(codevs2308)(luoguP2167) 状压dp

    唔...懒兔子来写博客了... 点我看题 这题的话...我想了很久但是都不是可行解 刚开始想预处理任意两个串是否可以匹配然后在乱搞,后来发现完全不会写... 然后按照惯例,我会看题解认真的思考... ...

  10. vue.js的一些事件绑定和表单数据双向绑定

    知识点: v-on:相当于: 例如:v-on:click==@click ,menthods事件绑定 v-on修饰符可以指定键盘事件 v-model进行表单数据的双向绑定 <template&g ...