使用存储过程进行验证

时间:2016-04-07 04:52:09

标签: sql-server stored-procedures

我在SQL Server中尝试使用存储过程进行验证。但存储过程无效。

我有一张桌子room。我从房间桌上得到了备份作为房间备份。我试图在SQL Server中使用存储过程验证,当room表记录大于房间备份表记录时,所以我需要重写房间备份表。我尝试了以下代码,但它无法正常工作

CREATE PROC Sp_verify
AS
    IF (SELECT Count()
        FROM   Room) > (SELECT Count()
                        FROM   RoomBackup)
      BEGIN
          DELETE RoomBackup;

          INSERT INTO RoomBackup
                      (R_No,
                       R_Type,
                       R_stus,
                       AP_No,
                       P_Name,
                       R_Charge)
          SELECT R_No,
                 R_Type,
                 R_stus,
                 AP_No,
                 P_Name,
                 R_Charge
          FROM   Room;
      END 

请帮帮我

1 个答案:

答案 0 :(得分:0)

修改程序如下:

IF ((SELECT Count(1)
        FROM   Room) > (SELECT Count(1)
                        FROM   RoomBackup))
      BEGIN
          DELETE RoomBackup;

          INSERT INTO RoomBackup
                      (R_No,
                       R_Type,
                       R_stus,
                       AP_No,
                       P_Name,
                       R_Charge)
          SELECT R_No,
                 R_Type,
                 R_stus,
                 AP_No,
                 P_Name,
                 R_Charge
          FROM   Room;
      END 

Count函数需要一个参数。 if子句

还需要括号括起来