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将查询的结果集一次性插入到表变量中的更多相关文章

  1. 【C#常用方法】3.将DataTable一次性插入数据库表中(使用SqlBulkCopy)

    将DataTable一次性插入数据库表中(使用SqlBulkCopy) 1.SqlBulkCopy简介 SqlBulkCopy类是ADO.NET中专门用于数据库批量插入数据的类,其批量插入的执行速度是 ...

  2. MySQL将表a中查询的数据插入到表b中

    MySQL将表a中查询的数据插入到表b中 假设表b存在 insert into b select * from a; 假设表b不存在 create table b as select * from a ...

  3. SQL知识整理一:触发器、存储过程、表变量、临时表

    触发器 触发器的基础知识 create trigger tr_name on table/view {for | after | instead of } [update][,][insert][,] ...

  4. sql server查询数据库的大小和各数据表的大小

    查询出来的结果中各字段的详细说明参考MSDN资料:https://msdn.microsoft.com/zh-cn/library/ms188776.aspx 如果只是查询数据库的大小的话,直接使用以 ...

  5. mysql查询-从表1中查询出来的结果重新插入到表1

    原有表结构 CREATE TABLE `t_card_user` ( `id` varchar(32) NOT NULL, `card_user_id` bigint(20) DEFAULT NULL ...

  6. SQL高级查询技巧(两次JOIN同一个表,自包含JOIN,不等JOIN)

    掌握了这些,就比较高级啦 Using the Same Table Twice 如下面查询中的branch字段 SELECT a.account_id, e.emp_id, b_a.name open ...

  7. SQL Server 查询某个字段值在哪张表的哪个字段

    我要查找值为‘WSCOL1525’的字段. declare @cloumns varchar(40)declare @tablename varchar(40)declare @str varchar ...

  8. sql 将表B中不存在表A的数据 插入到表A中

    insert into tableA select * from tableB b where not exists(select 1 from tableA a where a.id = b.id) ...

  9. 数据库表A中随机X条数据满足N条件的数据插入到表B中

    select  * into c FROM a TABLESAMPLE (5 PERCENT) select top 5 per * into c from a order by newid() se ...

随机推荐

  1. android studio你可能忽视的细节——启动白屏?drawable和mipmap出现的意义?这里都有!!!

    android studio用了很久了,也不知道各位小伙伴有没有还在用eclipse的,如果还有,楼主真心推荐转到android studio来吧,毕竟亲儿子,你会知道除了启动速度稍微慢些,你找不到一 ...

  2. 多线程条件通行工具——CountDownLatch

    CountDownLatch的作用是,线程进入等待后,需要计数器达到0才能通行. CountDownLatch(int)构造方法,指定初始计数. await()等待计数减至0. await(long, ...

  3. canvas快速绘制圆形、三角形、矩形、多边形

    想看前面整理的canvas常用API的同学可以点下面: canvas学习之API整理笔记(一) canvas学习之API整理笔记(二) 本系列文章涉及的所有代码都将上传至:项目代码github地址,喜 ...

  4. Storm

    2016-11-14  22:05:29 有哪些典型的Storm应用案例? 数据处理流:Storm可以用来处理源源不断流进来的消息,处理之后将结果写入到某个存储中去.不像其它的流处理系统,Storm不 ...

  5. 录像时调用MediaRecorder的start()时发生start failed: -19错误

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  6. .NET面试题系列[4] - C# 基础知识(2)

    2 类型转换 面试出现频率:主要考察装箱和拆箱.对于有笔试题的场合也可能会考一些基本的类型转换是否合法. 重要程度:10/10 CLR最重要的特性之一就是类型安全性.在运行时,CLR总是知道一个对象是 ...

  7. C#高级知识点&(ABP框架理论学习高级篇)——白金版

    前言摘要 很早以前就有要写ABP高级系列教程的计划了,但是迟迟到现在这个高级理论系列才和大家见面.其实这篇博客很早就着手写了,只是楼主一直写写停停.看看下图,就知道这篇博客的生产日期了,谁知它的出厂日 ...

  8. iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总

    --系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用 ...

  9. Web中的XHRHttpRequest

    1.提出者:Jesse James Garrett   2.IE中,XHR是通过ActiveX对象实现的.涉及浏览器的兼容性写法.   3.使用 <1>open("请求方式&qu ...

  10. Android动画效果之Property Animation进阶(属性动画)

    前言: 前面初步认识了Android的Property Animation(属性动画)Android动画效果之初识Property Animation(属性动画)(三),并且利用属性动画简单了补间动画 ...