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進程的詳細信息