--使用游标循环
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. 【Linux学习】1.Linux基础知识

    记录学习Linux 系统的相关知识点,欢迎大家拍砖交流,一起成长:QQ:2712192471 作者背景:前端开发工程师 | Python | web安全爱好者 一,Windows系统下 Linux 的 ...

  2. 【c++ primer, 5e】函数匹配

    练习 6.49 候选函数:与所调用的函数的名字相同的函数的集合. 可行函数:给候选函数加上参数数量.参数类型的约束所得到的函数的集合. 6.50 a 3.4可行,二义匹配 b 2.4可行,2是最佳匹配 ...

  3. Python面试题之回调函数

    0x00 概述 编程分为两类:系统编程(system programming)和应用编程(application programming).所谓系统编程,简单来说,就是编写库:而应用编程就是利用写好的 ...

  4. 20145302张薇《Java程序设计》第八周学习总结

    20145302 <Java程序设计>第八周学习总结 教材学习内容总结 第十五章 日志API简介 使用日志的起点是Logger类,要取得Logger类,必须使用Logger的静态方法get ...

  5. 扒开系统调用的三层皮(下)/给MenuOS增加time和time-asm命令

    上周从用户态的角度去理解系统调用 这周通过内核的方式 调试和跟踪系统调用来理解 rm menu -rf  强制删除原menu文件 git clone https://github.com/mengni ...

  6. 回到HTML〇

    HTML(HyperText Markup Language),用来向浏览器标示文档的所有“内容”与“结构”. 抱着温故而知新的态度,在这里通过“回到HTML”系列文章,重新梳理一下HTML的相关知识 ...

  7. 【网络结构】VGG-Net论文解析

    目录 0. 论文链接 1. 概述 2. 网络结构 2.1 卷积核 2.2 池化核 2.3 全连接层 3. 训练 4. 测试 5. 其他 6.参考链接 @ 0. 论文链接 论文链接 1. 概述   VG ...

  8. 03_Storm编程上手-wordcount

    1. Storm编程模型概要 消息源spout, 继承BaseRichSpout类 或 实现IRichSpout接口1)BaseRichSpout类相对比较简单,需要覆写的方法较少,满足基本业务需求2 ...

  9. NPM Scripts 2 -- rimraf copyfiles imagemin usemin htmlmin uglifyjs

    NPM Scripts Part 2 Objectives and Outcomes In this exercise you will learn to build a distribution f ...

  10. Eclipse解决乱码问题

    使用Eclipse的时候发现有些格式的文件会乱码单个文件设置了没问题,但是在项目上设置却不起作用. 解决方法如下: Windows -> Preferences -> General -& ...