我有一个包含多个记录的表:
我想创建一个触发器,以便没人可以两次提交相同问题的答案。它仅 是触发器 。
CREATE TRIGGER trigger_Check_Duplicates ON submit_Answer FOR INSERT AS IF SELECT??? PRINT 'duplicate' raiserror('cant submit answer to same question twice') ROLLBACK End
创建触发器
CREATE TRIGGER dbo.uniqueUserQuestion ON dbo.submit_Answer INSTEAD OF INSERT AS BEGIN SET NOCOUNT ON IF EXISTS ( SELECT 1 FROM dbo.submit_Answer T INNER JOIN INSERTED I ON T.user_name = I.user_name AND T.question_id = I.question_id ) BEGIN -- Do dupe handling here PRINT 'duplicate' raiserror('cant submit answer to same question twice') return END -- actually add it in INSERT INTO dbo.submit_Answer SELECT * FROM INSERTED I END GO