MS SQL存储过程无法正常工作

时间:2015-11-03 10:22:50

标签: sql sql-server tsql stored-procedures

我有一个名为Attendance的表,我使用first_term的存储过程数据将数据插入此表,它成功插入但是当我们为second_term插入数据时,它没有正确插入。

我的数据库表:

        /****** Object:  Table [dbo].[Attendance]    Script Date:   11/01/201******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Attendance](
  [attendanceid] [int] IDENTITY(1,1) NOT NULL,
  [admission_no] [nvarchar](50) NULL,
  [totalattendance_firstterm] [int] NULL,
  [totalworkingdays_firstterm] [int] NULL,
  [totalattendance_secondterm] [int] NULL,
  [totalworkingday_secondterm] [int] NULL
) ON [PRIMARY]
GO

我的存储过程如下:

  ALTER proc [dbo].[student_attendancedetails]
  (
  @admission_no nvarchar(50) ,
  @totalattendance_firstterm int,
  @totalworkingdays_firstterm int,
  @totalattendance_secondterm int,
  @totalworkingday_secondterm int
  )
 as 
 BEGIN
 IF NOT EXISTS(select admission_no from Attendance where admission_no=@admission_no)
 BEGIN
 INSERT INTO       Attendance(admission_no,totalattendance_firstterm,totalworkingdays_firstterm,   totalattendance_secondterm,totalworkingday_secondterm)values(@admission_no,@t   otalattendance_firstterm,@totalworkingdays_firstterm,@totalattendance_seconderm,@totalworkingday_secondterm)
END
ELSE IF(
(select totalattendance_firstterm from Attendance where   admission_no=@admission_no)is null or (select totalattendance_firstterm from   Attendance where admission_no=@admission_no)=0)
BEGIN
UPDATE Attendance set   totalattendance_firstterm=@totalattendance_firstterm where   admission_no=@admission_no
 END

ELSE IF(
  (select totalworkingdays_firstterm from Attendance where   admission_no=@admission_no)is null or (select totalworkingdays_firstterm  from Attendance where admission_no=@admission_no)=0)
  BEGIN
  UPDATE Attendance set    totalworkingdays_firstterm=@totalworkingdays_firstterm where      admission_no=@admission_no
  END

  ELSE IF(
   (select totalattendance_secondterm from Attendance where     admission_no=@admission_no)is null or (select totalattendance_secondterm    from Attendance where admission_no=@admission_no)=0)
  BEGIN
 UPDATE Attendance set   totalattendance_secondterm=@totalattendance_secondterm where     admission_no=@admission_no
  END
   ELSE IF(
   (select totalworkingday_secondterm from Attendance where    admission_no=@admission_no)is null or (select totalworkingday_secondterm from Attendance where admission_no=@admission_no)=0)
    BEGIN
    UPDATE Attendance set    totalworkingday_secondterm=@totalworkingday_secondterm where    admission_no=@admission_no
    END

  END

  GO

当我们按如下方式执行我的存储过程时:

 exec student_attendancedetails '2089',89,120,0,0
 exec student_attendancedetails '2089',0,0,98,150

它将给出以下结果:

enter image description here

但我希望我的结果如下:

enter image description here

1 个答案:

答案 0 :(得分:0)

答案非常简单。当你跑:

IF condition1
  code1
ELSE IF condition2
  code2
ELSE IF condition2
  code3
ELSE IF condition3
  code4
...

只会执行一个(code1/code2/code3/code4)。根据您的代码,我假设您要更新两次。所以你需要这样的东西:

IF condition1
BEGIN
  INSERT
END
ELSE
BEGIN
  IF condition2
    UPDATE1
  IF condition3
    UPDATE2
  IF condition4
   UPDATE3
END

LiveDemo

警告:

(select totalattendance_secondterm from Attendance where admission_no=@admission_no)=0
当您的查询返回多行时,

将失败。但您可能在admission_no上拥有唯一/主键。如果不是我建议添加它。