sql将查询的结果集一次性插入到表变量中
sql代码:
declare @Subject table
(--题目表变量
SubjectID int,
Question nvarchar(MAX),
CorrectAnswer varchar(100),
Explain nvarchar(MAX),
SubjectTypeID int,
CreateID int,
CreateDate datetime,
SubjectScore decimal(3, 1),
ScoreSort int
)
insert into @Subject(SubjectID,Question,CorrectAnswer,Explain,SubjectTypeID,CreateID,CreateDate,SubjectScore,ScoreSort)
select a.SubjectID,a.Question,a.CorrectAnswer,a.Explain,a.SubjectTypeID,a.CreateID,a.CreateDate,a.SubjectScore,a.ScoreSort from
(--题目表(传入参数 HistPaperID,subjecttitleid)
select a.SubjectID,Question,Answer as CorrectAnswer,Explain,TypeID as SubjectTypeID,a.CreateID,CreateDate,Score as SubjectScore,Sort as ScoreSort from HistPaperSubject a
left join HistPaperSubjectScore b on a.subjectID=b.subjectID
where a.isdel=0 and a.HistPaperID=60 and b.HistPaperID=60 and b.subjecttitleid=193) a
--select * from @Subject declare @StudentAnswer table
(--学生答题表变量
UserPaperID int,
UserID int,
SubmitDate datetime,
CreateDate datetime,
SubjectID int,
StudentAnswer varchar(100),
SubjectSort int,
StudentScore decimal(3, 1)
)
insert into @StudentAnswer(UserPaperID,UserID,SubmitDate,CreateDate,SubjectID,StudentAnswer,SubjectSort,StudentScore)
select b.UserPaperID,b.UserID,b.SubmitDate,b.CreateDate,b.SubjectID,b.StudentAnswer,b.SubjectSort,b.StudentScore from
(--题目表(传入参数 HistPaperID)
select UserPaperID,UserID,SubmitDate,CreateDate,SubjectID,Answer as StudentAnswer,sort as SubjectSort,Score as StudentScore from UserPaper a
left join UserPaperSubject b on a.id=b.userpaperid
where a.HistPaperID=60 and b.HistPaperID=60 and a.[status]=1 and a.isdel=0) b
--select * from @StudentAnswer declare @Result table
(--最终结果报表
SubjectID int,
Question nvarchar(MAX),
CorrectAnswer varchar(100),
Explain nvarchar(MAX),
SubjectTypeID int,
CreateID int,
CreateDate datetime,
SubjectScore decimal(3, 1),
ScoreSort int,
DeFenLv float,
[PerCent] varchar(20)
)
declare @SubjectID int,--题目ID
@CorrectAnswer varchar(100),--正确答案
@CorrectNum int,--正确的题目数
@TotalNum int,--总的题目数
@DeFenLv float,--得分率(以浮点数形式表示)
@PerCent varchar(20);--得分率(以百分比形式表示)
while EXISTS(select SubjectID from @Subject)--循环题目表变量
begin
select @SubjectID=SubjectID,@CorrectAnswer=CorrectAnswer from @Subject;
select @CorrectNum=Count(*) from @StudentAnswer where subjectid=@SubjectID and StudentAnswer=@CorrectAnswer--正确的题目数
select @TotalNum=Count(*) from @StudentAnswer where subjectid=@SubjectID--总的题目数
select @DeFenLv=convert(float,@CorrectNum)/convert(float,@TotalNum),@PerCent=cast(cast(round(convert(float,@CorrectNum)/convert(float,@TotalNum)*100,0) as decimal(18,0)) as varchar)+'%'
--最终表(题目及其得分率组成)
insert into @Result select a.SubjectID,a.Question,a.CorrectAnswer,a.Explain,a.SubjectTypeID,a.CreateID,a.CreateDate,a.SubjectScore,a.ScoreSort,@DeFenLv,@PerCent from @Subject a where a.subjectid=@SubjectID
delete from @Subject where subjectid=@SubjectID
end
select * from @Result
数据库死锁(查询或其他陷入死循环)
--停止死锁的进程:
declare @spid int
Set @spid = 57 --锁表进程
declare @sql varchar(1000)
set @sql='kill '+cast(@spid as varchar)
exec(@sql) select * from sys.sysprocesses where hostname='SALE-PC'--查看SQL进程的详细信息
sql将查询的结果集一次性插入到表变量中的更多相关文章
- 【C#常用方法】3.将DataTable一次性插入数据库表中(使用SqlBulkCopy)
将DataTable一次性插入数据库表中(使用SqlBulkCopy) 1.SqlBulkCopy简介 SqlBulkCopy类是ADO.NET中专门用于数据库批量插入数据的类,其批量插入的执行速度是 ...
- MySQL将表a中查询的数据插入到表b中
MySQL将表a中查询的数据插入到表b中 假设表b存在 insert into b select * from a; 假设表b不存在 create table b as select * from a ...
- SQL知识整理一:触发器、存储过程、表变量、临时表
触发器 触发器的基础知识 create trigger tr_name on table/view {for | after | instead of } [update][,][insert][,] ...
- sql server查询数据库的大小和各数据表的大小
查询出来的结果中各字段的详细说明参考MSDN资料:https://msdn.microsoft.com/zh-cn/library/ms188776.aspx 如果只是查询数据库的大小的话,直接使用以 ...
- mysql查询-从表1中查询出来的结果重新插入到表1
原有表结构 CREATE TABLE `t_card_user` ( `id` varchar(32) NOT NULL, `card_user_id` bigint(20) DEFAULT NULL ...
- SQL高级查询技巧(两次JOIN同一个表,自包含JOIN,不等JOIN)
掌握了这些,就比较高级啦 Using the Same Table Twice 如下面查询中的branch字段 SELECT a.account_id, e.emp_id, b_a.name open ...
- SQL Server 查询某个字段值在哪张表的哪个字段
我要查找值为‘WSCOL1525’的字段. declare @cloumns varchar(40)declare @tablename varchar(40)declare @str varchar ...
- sql 将表B中不存在表A的数据 插入到表A中
insert into tableA select * from tableB b where not exists(select 1 from tableA a where a.id = b.id) ...
- 数据库表A中随机X条数据满足N条件的数据插入到表B中
select * into c FROM a TABLESAMPLE (5 PERCENT) select top 5 per * into c from a order by newid() se ...
随机推荐
- 简记某WebGIS项目的优化之路
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1. 背景 该项目为研究生时的老师牵头,个人已毕业数年,应老师要求协助其 ...
- [转]Patch文件结构详解
N久不来 于是不知道扔在哪儿于是放这里先 如果你觉得碍事的话 帮我扔到合适的版块去.. 导读这是一篇说明文 它介绍了标准冒险岛更新文件(*.patch;*.exe)的格式文章的最后附了一段C#的参考代 ...
- iOS 数据存储之SQLite3的使用
SQLite3是iOS内嵌的数据库,SQLite3在存储和检索大量数据方面非常有效,它使得不必将每个对象都加到内存中.还能够对数据进行负责的聚合,与使用对象执行这些操作相比,获得结果的速度更快. SQ ...
- 流程表单中js如何清空SheetUser控件数据?
昨天有人问我js怎么清空.我试了试,发现简单的赋给他空值,并没有用.只能给他赋一个真实存在的值才有用.于是跟踪了一下他的删除按钮. 效果如下 使用场景:可以根据字段的不同类别变更人员. js代码如下, ...
- Android studio使用git教程
①下载Git工具,配置到Android studio中 http://git-scm.com/downloads ------------------------------------------- ...
- 使用四元数解决万向节锁(Gimbal Lock)问题
问题 使用四元数可以解决万向节锁的问题,但是我在实际使用中出现问题:我设计了一个程序,显示一个三维物体,用户可以输入绕zyx三个轴进行旋转的指令,物体进行相应的转动. 由于用户输入的是绕三个轴旋转的角 ...
- EChart系列:在echart3中使用百度地图扩展之后,如何获取到百度地图对象
最近做项目想要在百度地图上叠加显示echart的散点图,然后根据地图的缩放等级和区域范围要显示不同的散点图,这中间折腾了好久.功能要求包括: (1)底图使用百度地图: (2)可以在地图上叠加显示ech ...
- Android,适合Restful网络请求封装
借助volley.Gson类库. 优点 网络请求集中处理,返回值直接为预期的对象,不需要手动反序列,提高效率,使用时建立好model类即可. 使用效果 DataProess.Request(true, ...
- ABP框架 - 依赖注入
文档目录 本节内容: 什么是依赖注入 传统方式的问题 解决方案 构造器注入模式 属性注入模式 依赖注入框架 ABP 依赖注入基础 注册依赖 约定注入 辅助接口 自定义/直接 注册 使用IocManag ...
- Entity Framework 6 Recipes 2nd Edition(10-2)译 -> 返回输出参数
10-2. 返回输出参数 问题 想获取存储过程里的一个或多个输出参数的值 解决方案 假设我们有一个像Figure 10-1所示的,出租车辆与租金收入的模型 Figure 10-1.出租车辆与租金收入的 ...