触发器~~~
Student
XH XM RXRQ
601226 a 20040601
601227 b 20040601
StuZyB
XH ZID
601226 11
601227 11
StuXxxsB
XH XXXS
601226 夜大
601227 夜大
Course
ND ZID XXXS CID CName
2004 11 夜大 1111 网络
Score
CID XH CJ
我在Course插入 (2004,11,夜大,1111,网络)的时候,
要求能在Score中插入对应的(1111,601226) (1111,601227)
**********************************
CREATE TRIGGER Course_insert ON Course
FOR INSERT
AS
DECLARE @CND varchar(4)
DECLARE @CZID varchar(5)
DECLARE @CXXXS varchar(10)
DECLARE @CCID varchar(5)
INSERT INTO Score(CID,XH)
SELECT T.CID,StuZyB.XH FROM Student, StuZyB, StuXxxsB,INSERTED T
WHERE (Student.RXRQ LIKE T.ND+ '% ' ) AND (StuXxxsB.XXXS = T.XXXS) AND (StuZyB.ZID = T.ZID)
***************************************
可是在插入值的时候,Score弹出错误说不能插入重复值,我想应该是把同一个学号多次插入进去了,该如何更改触发器呢
------解决方案--------------------不能重复插入应该是主键的问题
------解决方案--------------------INSERT INTO Score(CID,XH)
SELECT T.CID,StuZyB.XH FROM Student, StuZyB, StuXxxsB,INSERTED T
WHERE (Student.RXRQ LIKE T.ND+ '% ' ) AND (StuXxxsB.XXXS = T.XXXS) AND (StuZyB.ZID = T.ZID)=========>
INSERT INTO Score(CID,XH)
SELECT T.CID,StuZyB.XH FROM Student, StuZyB, StuXxxsB,INSERTED T
WHERE Student.xh=StuZyB.xh and Student.xh=StuXxxsB.xh and
(Student.RXRQ LIKE T.ND+ '% ' ) AND (StuXxxsB.XXXS = T.XXXS) AND (StuZyB.ZID = T.ZID)
------解决方案--------------------INSERT INTO Score(CID,XH)
SELECT T.CID,StuZyB.XH FROM Student, StuZyB, INSERTED T
WHERE (Student.RXRQ LIKE T.ND+ '% ' ) AND (StuZyB.ZID = T.ZID) and student.xh = StuZyB.XH