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. [转载]强制不使用“兼容性视图”的HTML代码

    在IE8浏览器以后版本,都有一个"兼容性视图",让不少新技术无法使用.那么如何禁止浏览器自动选择"兼容性视图",强制IE以最高级别的可用模式显示内容呢?下面就介 ...

  2. PHP设计模式(七)适配器模式(Adapter For PHP)

    适配器模式:将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 如下图(借图): // 设置书的接口 // 书接口 interface BookI ...

  3. spider RPC入门指南

    本部分将介绍使用spider RPC开发分布式应用的客户端和服务端. spider RPC中间件基于J2SE 8开发,因此需要确保服务器上安装了JDK 8及以上版本,不依赖于任何额外需要独立安装和配置 ...

  4. CSS三个定位——常规、浮动、绝对定位

    .dage { width: 868px; background: #5B8C75; border: 10px solid #A08C5A; margin-top: -125px; margin-le ...

  5. i++、++i 、i--、--i

    总结: i++ 先用后加, ++i先加后用: i--先用后减, --i先减后用: //int i = 1; //Console.WriteLine(i);//1 //Console.WriteLine ...

  6. windows 7(32/64位)GHO安装指南(U盘引导篇)~

    上一篇我们说了怎么制作U盘启动盘,那么这一篇让我们来看看如何进行正确的U盘引导启动. 现在的个人计算机一般分为台式机和笔记本,由于各厂商的喜好不同(开玩笑的啦),所以对于主板的BIOS设置各所不同.进 ...

  7. Redux初见

    说到redux可能我们都先知道了react,但我发现,关于react相关的学习资料很多,也有各种各样的种类,但是关于redux简单易懂的资料却比较少. 这里记录一下自己的学习理解,希望可以简洁易懂,入 ...

  8. MapReduce剖析笔记之八: Map输出数据的处理类MapOutputBuffer分析

    在上一节我们分析了Child子进程启动,处理Map.Reduce任务的主要过程,但对于一些细节没有分析,这一节主要对MapOutputBuffer这个关键类进行分析. MapOutputBuffer顾 ...

  9. [转]Fiddler抓取Android真机上的HTTPS包

    此篇文章转载自:http://blog.csdn.net/roland_sun/article/details/30078353 工作中经常会需要对一些app进行抓包, 但是每次默认都是只抓http请 ...

  10. pod Spec管理配置

    pod Spec 为自己的项目添加pod管理功能.前言: 上一篇文章中提到,因为自己在操作的时候遇到很多坑,所在在此做一个记录,同样也希望可以帮到在这个操作上遇到坑的人. 本文将采用配图和加文字的方式 ...