Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi
Well basically I need this trigger to work after a user inserts multiple records into the database.

SQL
CREATE TRIGGER IntelligentThinkingDev
   ON AuditResults AFTER INSERT
AS 
BEGIN
	DECLARE @OPTIONID INT
	DECLARE @ISoptional INT
	DECLARE @AuditResul INT
	SELECT @OPTIONID =  OPTIONID FROM INSERTED
	SELECT @ISoptional =  ISoptional FROM INSERTED
	SET @AuditResult = CASE WHEN @optionID = 0 and @ISoptional = 1 THEN
	UPDATE AuditResults SET optionid = null WHERE ResultID = (SELECT ResultID FROM INSERTED)	
END


I am getting just the incorrect syntax near 'UPDATE'

Thank you in advance
Posted
Updated 19-Dec-13 21:28pm
v2
Comments
Maciej Los 19-Dec-13 11:57am    
Not clear! Please, be more specific and provide more details...

No trigger works on "multiple records". Each time a record is inserted, the trigger will be run. If you insert 10 records, the trigger will run 10 times - once for each inserted record as it is being inserted.
 
Share this answer
 
Comments
[no name] 19-Dec-13 17:57pm    
Short and clear and how it is
Member 9374423 20-Dec-13 1:50am    
Thank you, Now I know
Try this one.. if you are trying to update the optionid of the currently inserted data.
SQL
CREATE TRIGGER ThisDatabase
   ON OtherTable AFTER INSERT
AS 
BEGIN
	DECLARE @OPTIONID INT
	DECLARE @ISoptional INT
	SET @OPTIONID = SELECT OPTIONID FROM INSERTED
	SET @ISoptional = SELECT ISoptional FROM INSERTED
	CASE WHEN @optionID = 0 and @ISoptional = 1 THEN
	BEGIN
		UPDATE OtherTable SET optionid = null WHERE YourPrimaryKey = (SELECT YourPrimaryKey FROM INSERTED)
	END
END
 
Share this answer
 
Comments
Member 9374423 20-Dec-13 0:21am    
Incorrect syntax near the selects and the case?
Member 9374423 20-Dec-13 3:20am    
this resolved the issue with the selects

CREATE TRIGGER IntelligentThinkingDev
ON AuditResults AFTER INSERT
AS

BEGIN
DECLARE @OPTIONID INT
DECLARE @ISoptional INT
SELECT @OPTIONID = OPTIONID FROM INSERTED
SELECT @ISoptional = ISoptional FROM INSERTED

CASE WHEN @optionID = 0 and @ISoptional = 1 THEN
BEGIN
UPDATE AuditResults SET optionid = null WHERE ResultID = (SELECT ResultID FROM INSERTED)
END
END

but I still have an issue with the case error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900