T-SQL Update Trigger
up vote
0
down vote
favorite
I'm trying to create the following trigger in SQL Server, but SSMS throws an error and I have no clue what it is. Any thoughts ?
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'trigger'.
Code:
IF NOT EXISTS(SELECT * FROM sys.triggers 
              WHERE object_id = OBJECT_ID(N'[dbo].[trAfterUpdateInfoDoc]'))
    CREATE TRIGGER [dbo].[trAfterUpdateInfoDoc]
    ON [dbo].[InfoDocs]
    AFTER UPDATE
    AS
    BEGIN
        DECLARE @infodoctemplateid INT;
        DECLARE @infodocid INT;
        DECLARE @requireccount FLOAT(2);
        DECLARE @filledcount FLOAT(2);
        DECLARE @pcnt FLOAT(2);
        DECLARE c CURSOR FOR
             SELECT id 
             FROM InfoDocs ifd 
             WHERE exists (SELECT 1 FROM Inserted AS i WHERE i.id = ifd.id)
        OPEN c
        FETCH NEXT FROM c INTO @infodocid
        WHILE @@Fetch_Status = 0 
        BEGIN
            SELECT @infodoctemplateid = InfoDocTemplateId 
            FROM InfoDocs 
            WHERE id = @infodocid;
            SELECT @requireccount = COUNT(*) 
            FROM InfoDocTemplateFields 
            WHERE InfoDocTemplateId = @infodoctemplateid 
              AND IsRequired = 1;
            IF (@requireccount = 0)
            BEGIN
                set @pcnt = 100;
            END
            ELSE
            BEGIN
                select @filledcount = count(*) from InfoDocFields 
                where InfoDocId = @infodocid 
                and InfoDocTemplateFieldId in (select id from InfoDocTemplateFields where InfoDocTemplateId = @infodoctemplateid and IsRequired = 1)
                and (BooleanValue is not null or (StringValue is not null and StringValue <> '') or IntValue is not null or DateValue is not null)
                set @pcnt = @filledcount / @requireccount * 100.0;
            END
            update InfoDocs set PercentageCompleted = @pcnt Where id = @infodocid;
            Fetch next From c into @infodocid
        End
    Close c
    Deallocate c
END
add a comment |
up vote
0
down vote
favorite
I'm trying to create the following trigger in SQL Server, but SSMS throws an error and I have no clue what it is. Any thoughts ?
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'trigger'.
Code:
IF NOT EXISTS(SELECT * FROM sys.triggers 
              WHERE object_id = OBJECT_ID(N'[dbo].[trAfterUpdateInfoDoc]'))
    CREATE TRIGGER [dbo].[trAfterUpdateInfoDoc]
    ON [dbo].[InfoDocs]
    AFTER UPDATE
    AS
    BEGIN
        DECLARE @infodoctemplateid INT;
        DECLARE @infodocid INT;
        DECLARE @requireccount FLOAT(2);
        DECLARE @filledcount FLOAT(2);
        DECLARE @pcnt FLOAT(2);
        DECLARE c CURSOR FOR
             SELECT id 
             FROM InfoDocs ifd 
             WHERE exists (SELECT 1 FROM Inserted AS i WHERE i.id = ifd.id)
        OPEN c
        FETCH NEXT FROM c INTO @infodocid
        WHILE @@Fetch_Status = 0 
        BEGIN
            SELECT @infodoctemplateid = InfoDocTemplateId 
            FROM InfoDocs 
            WHERE id = @infodocid;
            SELECT @requireccount = COUNT(*) 
            FROM InfoDocTemplateFields 
            WHERE InfoDocTemplateId = @infodoctemplateid 
              AND IsRequired = 1;
            IF (@requireccount = 0)
            BEGIN
                set @pcnt = 100;
            END
            ELSE
            BEGIN
                select @filledcount = count(*) from InfoDocFields 
                where InfoDocId = @infodocid 
                and InfoDocTemplateFieldId in (select id from InfoDocTemplateFields where InfoDocTemplateId = @infodoctemplateid and IsRequired = 1)
                and (BooleanValue is not null or (StringValue is not null and StringValue <> '') or IntValue is not null or DateValue is not null)
                set @pcnt = @filledcount / @requireccount * 100.0;
            END
            update InfoDocs set PercentageCompleted = @pcnt Where id = @infodocid;
            Fetch next From c into @infodocid
        End
    Close c
    Deallocate c
END
2
I don't know why you get a syntax error, but cursors should be only used as a last resort since they are performance killers, and in triggers they should be avoided almost at all costs. You should rewrite your trigger to work on a set based approach.
– Zohar Peled
Nov 8 at 17:59
Can you point me to an alternative for looping over a set and not using cursors
– BrilBroeder
Nov 8 at 18:33
Perhaps, but this is a different question - and should be on a different post. I know it seems petty and annoying at first, but when you think about it, the question You've asked was already answered by Martin Smith - so you should accept it and move on to the next question.
– Zohar Peled
Nov 8 at 18:41
1
create a new case as requested : stackoverflow.com/questions/53222867/…
– BrilBroeder
Nov 9 at 9:20
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to create the following trigger in SQL Server, but SSMS throws an error and I have no clue what it is. Any thoughts ?
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'trigger'.
Code:
IF NOT EXISTS(SELECT * FROM sys.triggers 
              WHERE object_id = OBJECT_ID(N'[dbo].[trAfterUpdateInfoDoc]'))
    CREATE TRIGGER [dbo].[trAfterUpdateInfoDoc]
    ON [dbo].[InfoDocs]
    AFTER UPDATE
    AS
    BEGIN
        DECLARE @infodoctemplateid INT;
        DECLARE @infodocid INT;
        DECLARE @requireccount FLOAT(2);
        DECLARE @filledcount FLOAT(2);
        DECLARE @pcnt FLOAT(2);
        DECLARE c CURSOR FOR
             SELECT id 
             FROM InfoDocs ifd 
             WHERE exists (SELECT 1 FROM Inserted AS i WHERE i.id = ifd.id)
        OPEN c
        FETCH NEXT FROM c INTO @infodocid
        WHILE @@Fetch_Status = 0 
        BEGIN
            SELECT @infodoctemplateid = InfoDocTemplateId 
            FROM InfoDocs 
            WHERE id = @infodocid;
            SELECT @requireccount = COUNT(*) 
            FROM InfoDocTemplateFields 
            WHERE InfoDocTemplateId = @infodoctemplateid 
              AND IsRequired = 1;
            IF (@requireccount = 0)
            BEGIN
                set @pcnt = 100;
            END
            ELSE
            BEGIN
                select @filledcount = count(*) from InfoDocFields 
                where InfoDocId = @infodocid 
                and InfoDocTemplateFieldId in (select id from InfoDocTemplateFields where InfoDocTemplateId = @infodoctemplateid and IsRequired = 1)
                and (BooleanValue is not null or (StringValue is not null and StringValue <> '') or IntValue is not null or DateValue is not null)
                set @pcnt = @filledcount / @requireccount * 100.0;
            END
            update InfoDocs set PercentageCompleted = @pcnt Where id = @infodocid;
            Fetch next From c into @infodocid
        End
    Close c
    Deallocate c
END
I'm trying to create the following trigger in SQL Server, but SSMS throws an error and I have no clue what it is. Any thoughts ?
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'trigger'.
Code:
IF NOT EXISTS(SELECT * FROM sys.triggers 
              WHERE object_id = OBJECT_ID(N'[dbo].[trAfterUpdateInfoDoc]'))
    CREATE TRIGGER [dbo].[trAfterUpdateInfoDoc]
    ON [dbo].[InfoDocs]
    AFTER UPDATE
    AS
    BEGIN
        DECLARE @infodoctemplateid INT;
        DECLARE @infodocid INT;
        DECLARE @requireccount FLOAT(2);
        DECLARE @filledcount FLOAT(2);
        DECLARE @pcnt FLOAT(2);
        DECLARE c CURSOR FOR
             SELECT id 
             FROM InfoDocs ifd 
             WHERE exists (SELECT 1 FROM Inserted AS i WHERE i.id = ifd.id)
        OPEN c
        FETCH NEXT FROM c INTO @infodocid
        WHILE @@Fetch_Status = 0 
        BEGIN
            SELECT @infodoctemplateid = InfoDocTemplateId 
            FROM InfoDocs 
            WHERE id = @infodocid;
            SELECT @requireccount = COUNT(*) 
            FROM InfoDocTemplateFields 
            WHERE InfoDocTemplateId = @infodoctemplateid 
              AND IsRequired = 1;
            IF (@requireccount = 0)
            BEGIN
                set @pcnt = 100;
            END
            ELSE
            BEGIN
                select @filledcount = count(*) from InfoDocFields 
                where InfoDocId = @infodocid 
                and InfoDocTemplateFieldId in (select id from InfoDocTemplateFields where InfoDocTemplateId = @infodoctemplateid and IsRequired = 1)
                and (BooleanValue is not null or (StringValue is not null and StringValue <> '') or IntValue is not null or DateValue is not null)
                set @pcnt = @filledcount / @requireccount * 100.0;
            END
            update InfoDocs set PercentageCompleted = @pcnt Where id = @infodocid;
            Fetch next From c into @infodocid
        End
    Close c
    Deallocate c
END
edited Nov 8 at 17:58
marc_s
565k12610921244
565k12610921244
asked Nov 8 at 17:52
BrilBroeder
364315
364315
2
I don't know why you get a syntax error, but cursors should be only used as a last resort since they are performance killers, and in triggers they should be avoided almost at all costs. You should rewrite your trigger to work on a set based approach.
– Zohar Peled
Nov 8 at 17:59
Can you point me to an alternative for looping over a set and not using cursors
– BrilBroeder
Nov 8 at 18:33
Perhaps, but this is a different question - and should be on a different post. I know it seems petty and annoying at first, but when you think about it, the question You've asked was already answered by Martin Smith - so you should accept it and move on to the next question.
– Zohar Peled
Nov 8 at 18:41
1
create a new case as requested : stackoverflow.com/questions/53222867/…
– BrilBroeder
Nov 9 at 9:20
add a comment |
2
I don't know why you get a syntax error, but cursors should be only used as a last resort since they are performance killers, and in triggers they should be avoided almost at all costs. You should rewrite your trigger to work on a set based approach.
– Zohar Peled
Nov 8 at 17:59
Can you point me to an alternative for looping over a set and not using cursors
– BrilBroeder
Nov 8 at 18:33
Perhaps, but this is a different question - and should be on a different post. I know it seems petty and annoying at first, but when you think about it, the question You've asked was already answered by Martin Smith - so you should accept it and move on to the next question.
– Zohar Peled
Nov 8 at 18:41
1
create a new case as requested : stackoverflow.com/questions/53222867/…
– BrilBroeder
Nov 9 at 9:20
2
2
I don't know why you get a syntax error, but cursors should be only used as a last resort since they are performance killers, and in triggers they should be avoided almost at all costs. You should rewrite your trigger to work on a set based approach.
– Zohar Peled
Nov 8 at 17:59
I don't know why you get a syntax error, but cursors should be only used as a last resort since they are performance killers, and in triggers they should be avoided almost at all costs. You should rewrite your trigger to work on a set based approach.
– Zohar Peled
Nov 8 at 17:59
Can you point me to an alternative for looping over a set and not using cursors
– BrilBroeder
Nov 8 at 18:33
Can you point me to an alternative for looping over a set and not using cursors
– BrilBroeder
Nov 8 at 18:33
Perhaps, but this is a different question - and should be on a different post. I know it seems petty and annoying at first, but when you think about it, the question You've asked was already answered by Martin Smith - so you should accept it and move on to the next question.
– Zohar Peled
Nov 8 at 18:41
Perhaps, but this is a different question - and should be on a different post. I know it seems petty and annoying at first, but when you think about it, the question You've asked was already answered by Martin Smith - so you should accept it and move on to the next question.
– Zohar Peled
Nov 8 at 18:41
1
1
create a new case as requested : stackoverflow.com/questions/53222867/…
– BrilBroeder
Nov 9 at 9:20
create a new case as requested : stackoverflow.com/questions/53222867/…
– BrilBroeder
Nov 9 at 9:20
add a comment |
                                2 Answers
                                2
                        
active
oldest
votes
up vote
3
down vote
accepted
Create Trigger (Limitations section) must be the first statement in a batch, so you can't use the IF exists check before it.
In SQL Server 2016 SP1 onwards, you can use CREATE OR ALTER TRIGGER... for the same behaviour.
Pre-SQL Server 2016 SP1, there's some suggestions here
I also second Zohar's comment that putting this logic into a trigger could well cause you many performance issues & possibly hard to track down unexpected behaviour/bugs.
add a comment |
up vote
2
down vote
Anytime a SQL object like a trigger is created, it needs to be the only object created in the batch.  A batch is terminated by the keyword GO.
Try refactoring your code to fit this general structure and see if it works:
IF OBJECT_ID(N'[dbo].[trAfterUpdateInfoDoc]') IS NOT NULL
    DROP TRIGGER [dbo].[trAfterUpdateInfoDoc]
GO
CREATE TRIGGER [dbo].[trAfterUpdateInfoDoc]
ON [dbo].[InfoDocs]
AFTER UPDATE
AS
BEGIN
    --PLACE CODE HERE
END
GO
add a comment |
                                2 Answers
                                2
                        
active
oldest
votes
                                2 Answers
                                2
                        
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Create Trigger (Limitations section) must be the first statement in a batch, so you can't use the IF exists check before it.
In SQL Server 2016 SP1 onwards, you can use CREATE OR ALTER TRIGGER... for the same behaviour.
Pre-SQL Server 2016 SP1, there's some suggestions here
I also second Zohar's comment that putting this logic into a trigger could well cause you many performance issues & possibly hard to track down unexpected behaviour/bugs.
add a comment |
up vote
3
down vote
accepted
Create Trigger (Limitations section) must be the first statement in a batch, so you can't use the IF exists check before it.
In SQL Server 2016 SP1 onwards, you can use CREATE OR ALTER TRIGGER... for the same behaviour.
Pre-SQL Server 2016 SP1, there's some suggestions here
I also second Zohar's comment that putting this logic into a trigger could well cause you many performance issues & possibly hard to track down unexpected behaviour/bugs.
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Create Trigger (Limitations section) must be the first statement in a batch, so you can't use the IF exists check before it.
In SQL Server 2016 SP1 onwards, you can use CREATE OR ALTER TRIGGER... for the same behaviour.
Pre-SQL Server 2016 SP1, there's some suggestions here
I also second Zohar's comment that putting this logic into a trigger could well cause you many performance issues & possibly hard to track down unexpected behaviour/bugs.
Create Trigger (Limitations section) must be the first statement in a batch, so you can't use the IF exists check before it.
In SQL Server 2016 SP1 onwards, you can use CREATE OR ALTER TRIGGER... for the same behaviour.
Pre-SQL Server 2016 SP1, there's some suggestions here
I also second Zohar's comment that putting this logic into a trigger could well cause you many performance issues & possibly hard to track down unexpected behaviour/bugs.
edited Nov 8 at 21:34
marc_s
565k12610921244
565k12610921244
answered Nov 8 at 18:02
Gareth Lyons
1,404413
1,404413
add a comment |
add a comment |
up vote
2
down vote
Anytime a SQL object like a trigger is created, it needs to be the only object created in the batch.  A batch is terminated by the keyword GO.
Try refactoring your code to fit this general structure and see if it works:
IF OBJECT_ID(N'[dbo].[trAfterUpdateInfoDoc]') IS NOT NULL
    DROP TRIGGER [dbo].[trAfterUpdateInfoDoc]
GO
CREATE TRIGGER [dbo].[trAfterUpdateInfoDoc]
ON [dbo].[InfoDocs]
AFTER UPDATE
AS
BEGIN
    --PLACE CODE HERE
END
GO
add a comment |
up vote
2
down vote
Anytime a SQL object like a trigger is created, it needs to be the only object created in the batch.  A batch is terminated by the keyword GO.
Try refactoring your code to fit this general structure and see if it works:
IF OBJECT_ID(N'[dbo].[trAfterUpdateInfoDoc]') IS NOT NULL
    DROP TRIGGER [dbo].[trAfterUpdateInfoDoc]
GO
CREATE TRIGGER [dbo].[trAfterUpdateInfoDoc]
ON [dbo].[InfoDocs]
AFTER UPDATE
AS
BEGIN
    --PLACE CODE HERE
END
GO
add a comment |
up vote
2
down vote
up vote
2
down vote
Anytime a SQL object like a trigger is created, it needs to be the only object created in the batch.  A batch is terminated by the keyword GO.
Try refactoring your code to fit this general structure and see if it works:
IF OBJECT_ID(N'[dbo].[trAfterUpdateInfoDoc]') IS NOT NULL
    DROP TRIGGER [dbo].[trAfterUpdateInfoDoc]
GO
CREATE TRIGGER [dbo].[trAfterUpdateInfoDoc]
ON [dbo].[InfoDocs]
AFTER UPDATE
AS
BEGIN
    --PLACE CODE HERE
END
GO
Anytime a SQL object like a trigger is created, it needs to be the only object created in the batch.  A batch is terminated by the keyword GO.
Try refactoring your code to fit this general structure and see if it works:
IF OBJECT_ID(N'[dbo].[trAfterUpdateInfoDoc]') IS NOT NULL
    DROP TRIGGER [dbo].[trAfterUpdateInfoDoc]
GO
CREATE TRIGGER [dbo].[trAfterUpdateInfoDoc]
ON [dbo].[InfoDocs]
AFTER UPDATE
AS
BEGIN
    --PLACE CODE HERE
END
GO
answered Nov 8 at 18:24
mjcataldi
515
515
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53213489%2ft-sql-update-trigger%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
I don't know why you get a syntax error, but cursors should be only used as a last resort since they are performance killers, and in triggers they should be avoided almost at all costs. You should rewrite your trigger to work on a set based approach.
– Zohar Peled
Nov 8 at 17:59
Can you point me to an alternative for looping over a set and not using cursors
– BrilBroeder
Nov 8 at 18:33
Perhaps, but this is a different question - and should be on a different post. I know it seems petty and annoying at first, but when you think about it, the question You've asked was already answered by Martin Smith - so you should accept it and move on to the next question.
– Zohar Peled
Nov 8 at 18:41
1
create a new case as requested : stackoverflow.com/questions/53222867/…
– BrilBroeder
Nov 9 at 9:20