当前位置: 代码迷 >> Sql Server >> 创造触发器时出错,求解
  详细解决方案

创造触发器时出错,求解

热度:43   发布时间:2016-04-27 12:06:19.0
创建触发器时出错,求解!
CREATE TRIGGER dbo.ck_credit 
  ON dbo.sc 
  AFTER INSERT
AS 
DECLARE @tatal tinyint 
SELECT @tatal=sum(credit) 
FROM (
  SELECT sno,credit
  FROM sc,course
  WHERE sc.sno=inserted.sno AND sc.sno=course.sno)
IF @tatal > 30 
  BEGIN
  ROLLBACK
  RAISERROR('错误提示',16,1)
  END
GO


提示错误如下:
消息 156,级别 15,状态 1,过程 ck_credit,第 12 行
关键字 'IF' 附近有语法错误。
找不到错误原因,求解惑,谢谢!

------解决方案--------------------
CREATE TRIGGER dbo.ck_credit
ON dbo.sc
AFTER INSERT
AS
DECLARE @tatal tinyint
SELECT @tatal=sum(credit)
FROM (
SELECT sno,credit
FROM sc,course
WHERE sc.sno=inserted.sno AND sc.sno=course.sno) A
IF @tatal > 30
BEGIN
ROLLBACK
RAISERROR('错误提示',16,1)
END
GO
------解决方案--------------------
SQL code
CREATE TRIGGER dbo.ck_credit    ON dbo.sc    AFTER INSERTAS  DECLARE @tatal tinyint  SELECT @tatal=sum(credit)  FROM (  SELECT sno,credit  FROM sc,course  WHERE sc.sno=inserted.sno AND sc.sno=course.sno)tIF @tatal > 30    BEGIN  ROLLBACK  RAISERROR('错误提示',16,1)  ENDGO
------解决方案--------------------
CREATE TRIGGER dbo.ck_credit
ON dbo.sc
AFTER INSERT
AS
DECLARE @tatal tinyint
SELECT @tatal=sum(credit)
FROM (
SELECT sno,credit
FROM sc,course,inserted
WHERE sc.sno=inserted.sno AND sc.sno=course.sno) A
IF @tatal > 30
BEGIN
ROLLBACK
RAISERROR('错误提示',16,1)
END
GO
------解决方案--------------------
SQL code
---查询表要取别名aCREATE TRIGGER dbo.ck_credit    ON dbo.sc    AFTER INSERTAS  DECLARE @tatal tinyint  SELECT @tatal=sum(credit)  FROM (  SELECT sno,credit  FROM sc,course  WHERE sc.sno=inserted.sno AND sc.sno=course.sno)aIF @tatal > 30    BEGIN  ROLLBACK  RAISERROR('错误提示',16,1)  ENDGO
------解决方案--------------------
CREATE TRIGGER dbo.ck_credit 
ON dbo.sc 
AFTER INSERT
AS 
--DECLARE @tatal tinyint 
--SELECT @tatal=sum(credit) 
IF ( SELECT sum(T.credit)
FROM (
SELECT sno,credit
FROM sc,course,inserted
WHERE sc.sno=inserted.sno AND 
sc.cno=course.cno) AS T) >30
--IF @tatal > 30 
BEGIN
ROLLBACK
RAISERROR('错误提示',16,1)
END
GO
------解决方案--------------------
SQL code
CREATE TRIGGER dbo.ck_credit  ON dbo.sc  AFTER INSERTAS  --DECLARE @tatal tinyint  IF (SELECT sum(credit所在的表名.credit)  FROM (  SELECT sno,credit  FROM sc,course,inserted--少了一个表啊,我给你加上你又不看  WHERE sc.sno=inserted.sno AND sc.sno=course.sno) as T)>30BEGIN  ROLLBACK  RAISERROR('错误提示',16,1)ENDGO
  相关解决方案