我使用Microsoft SQL 2012快递版进行学术考勤监控项目,在这里我与您分享我今天遇到的一个奇怪的情况,无法弄清楚!希望得到你的帮助。
请注意程序的上半部分是完美的,直到更新@final_result
SQL只是忽略了苞片!!。
解决方法我已经尝试过以下方法:
将更新查询放在'if语句'括号之外,它运行正常!! 那么为什么它在'if语句'括号内不起作用?
将更新查询与'case statement'一起使用,但它不起作用!
为更新查询创建不同的变量只使用(我知道它没有意义!但无论如何我不知道是什么问题)
我已将我的整个程序发布到您的审核中,我希望这不是一个小错误,因为我已经花了这么多时间。
declare @class_id int = null,
@badge varchar(8) = null,
@name varchar(100) = null,
@enrollment_id int = null,
@absence_date datetime = null
set @absence_date = '2015-11-4'
set @badge = '100100'
set @class_id = 1039
BEGIN
Declare @Period1 bit = null,
@Period2 bit = null,
@Period3 bit = null,
@Period4 bit = null,
@Period5 bit = null,
@Period6 bit = null,
@Period7 bit = null,
@Period8 bit = null,
@remarks varchar(100) = null,
@show_Period1 bit = null,
@show_Period2 bit = null,
@show_Period3 bit = null,
@show_Period4 bit = null,
@show_Period5 bit = null,
@show_Period6 bit = null,
@show_Period7 bit = null,
@show_Period8 bit = null
SET NOCOUNT ON;
Declare @final_result table(Student_Badge varchar(8), Full_Name varchar(100), Enrollment_id int
, Period1 bit, Period2 bit, Period3 bit, Period4 bit, Period5 bit, Period6 bit
, Period7 bit, Period8 bit, Remarks varchar(100))
select A.Student_badge, B.Full_Name, A.Enrollment_id into #tempStudent from tb_Enrollments as A
left outer join tb_Students as B on A.Student_badge=B.Student_Badge where A.Class_id=@class_id and A.Active=1
while (Select count(*) from #tempStudent) > 0
BEGIN
Select top 1 @badge=Student_badge,@name=Full_Name,@enrollment_id=Enrollment_id from #tempStudent
if ( (select count(*) from tb_Absence as A left outer join tb_Enrollments as B on A.Enrollment_ID=B.Enrollment_id
where A.Absence_date=@absence_Date and A.Student_Badge=@badge and B.Class_id=@class_id) > 0)
BEGIN
-- if there is absence record for this student
Select @Period1=A.Period1, @Period2=A.Period2, @Period3=A.Period3, @Period4=A.Period4,
@Period5=A.Period5, @Period6=A.Period6, @Period7=A.Period7, @Period8=A.Period8, @remarks=A.Remarks
from tb_Absence as A left outer join tb_Enrollments as B on A.Enrollment_ID=B.Enrollment_id
where A.Absence_date=@absence_Date and A.Student_Badge=@badge and B.Class_id=@class_id
and A.Enrollment_ID=@enrollment_id
END
ELSE
BEGIN
-- if there is no absence record for this student
Set @Period1 = 0 set @Period2 = 0 set @Period3 = 0 set @Period4 = 0
set @Period5 = 0 set @Period6 = 0 set @Period7 = 0 set @Period8 = 0 set @remarks = ''
END
insert into @final_result values(@badge,@name,@enrollment_id,@Period1,@Period2,@Period3,@Period4
,@Period5,@Period6,@Period7,@Period8,@remarks)
delete #tempStudent where Student_badge=@badge
END
drop table #tempStudent
class_id,Period1,Period2,Period3,Period4,Period5,Period6,Period7,Period8
into #tempclasses from tb_Classes where Class_ID=@class_id and active=1
Select @show_Period1=Period1, @show_Period2=Period2, @show_Period3=Period3, @show_Period4=Period4,
@show_Period5=Period5, @show_Period6=Period6, @show_Period7=Period7, @show_Period8=Period8 from #tempclasses
-- issue started from here !!
if @show_Period1 = 0
BEGIN
update @final_result set Period1 = 0
END
if @show_Period2 = 0
BEGIN
update @final_result set Period2 = 0
END
if @show_Period3 = 0
BEGIN
update @final_result set Period3 = 0
END
if @show_Period4 = 0
BEGIN
update @final_result set Period4 = 0
END
if @show_Period5 = 0
BEGIN
update @final_result set Period5 = 0
END
if @show_Period6 = 0
BEGIN
update @final_result set Period6 = 0
END
if @show_Period7 = 0
BEGIN
update @final_result set Period7 = 0
END
if (@show_Period8 = 0)
BEGIN
update @final_result set Period8 = 0
END
drop table #tempclasses
select * from @final_result
END