日期:2014-05-18  浏览次数:20902 次

创建触发器时出错,求解!
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 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)t
IF @tatal > 30  
  BEGIN
  ROLLBACK
  RAISERROR('错误提示',16,1)
  END
GO

------解决方案--------------------
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
---查询表要取别名a
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

------解决方案--------------------
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 INSERT
AS  
--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)>30
BEGIN
  ROLLBACK
  RAISERROR('错误提示',16,1)
END
GO