Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 234859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:13:09+00:00 2026-05-11T20:13:09+00:00

I’ve got a trigger attached to a table. ALTER TRIGGER [dbo].[UpdateUniqueSubjectAfterInsertUpdate] ON [dbo].[Contents] AFTER

  • 0

I’ve got a trigger attached to a table.

ALTER TRIGGER [dbo].[UpdateUniqueSubjectAfterInsertUpdate]
   ON  [dbo].[Contents]
   AFTER INSERT,UPDATE
AS
BEGIN

-- Grab the Id of the row just inserted/updated
DECLARE @Id INT

SELECT @Id = Id
FROM INSERTED

END

Every time a new entry is inserted or modified, I wish to update a single field (in this table). For the sake of this question, imagine i’m updating a LastModifiedOn (datetime) field.

Ok, so what i’ve got is a batch insert thingy..

INSERT INTO [dbo].[Contents]
SELECT Id, a, b, c, d, YouDontKnowMe
FROM [dbo].[CrapTable]

Now all the rows are correctly inserted. The LastModifiedOn field defaults to null. So all the entries for this are null — EXCEPT the first row.

Does this mean that the trigger is NOT called for each row that is inserted into the table, but once AFTER the insert query is finished, ie. ALL the rows are inserted? Which mean, the INSERTED table (in the trigger) has not one, but ‘n’ number of rows?!

If so .. er.. 🙁 Would that mean i would need a cursor in this trigger? (if i need to do some unique logic to each single row, which i do currently).

?

UPDATE

I’ll add the full trigger code, to see if it’s possible to do it without a cursor.

BEGIN
    SET NOCOUNT ON

    DECLARE @ContentId INTEGER,
        @ContentTypeId TINYINT,
        @UniqueSubject NVARCHAR(200),
        @NumberFound INTEGER
    
    -- Grab the Id. Also, convert the subject to a (first pass, untested)
    -- unique subject.
    -- NOTE: ToUriCleanText just replaces bad uri chars with a ''. 
    --   eg. an '#' -> ''
    SELECT @ContentId = ContentId, @ContentTypeId = ContentTypeId, 
        @UniqueSubject = [dbo].[ToUriCleanText]([Subject])
    FROM INSERTED
    
    -- Find out how many items we have, for these two keys.
    SELECT @NumberFound = COUNT(ContentId)
    FROM [dbo].[Contents]
    WHERE ContentId = @ContentId
        AND UniqueSubject = @UniqueSubject
    
    -- If we have at least one identical subject, then we need to make it 
    -- unique by appending the current found number.
    -- Eg. The first instance has no number. 
    --     Second instance has subject + '1',
    --     Third instance has subject + '2', etc...
    IF @NumberFound > 0
        SET @UniqueSubject = @UniqueSubject + CAST(@NumberFound AS NVARCHAR(10))

    -- Now save this change.
    UPDATE [dbo].[Contents]
    SET UniqueSubject = @UniqueSubject
    WHERE ContentId = @ContentId
END
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:13:09+00:00Added an answer on May 11, 2026 at 8:13 pm

    Why not change the trigger to deal with multiple rows?
    No cursor or loops needed: it’s the whole point of SQL …

    UPDATE
        dbo.SomeTable
    SET
        LastModifiedOn = GETDATE()
    WHERE
        EXIST (SELECT * FROM INSERTED I WHERE I.[ID] = dbo.SomeTable.[ID]
    

    Edit: Something like…

    INSERT @ATableVariable
        (ContentId, ContentTypeId, UniqueSubject)
    SELECT 
        ContentId, ContentTypeId, [dbo].[ToUriCleanText]([Subject])
    FROM
        INSERTED
    
    UPDATE
        [dbo].[Contents]
    SET
        UniqueSubject + CAST(NumberFound AS NVARCHAR(10))
    FROM
        --Your original COUNT feels wrong and/or trivial
        --Do you expect 0, 1 or many rows.
        --Edit2: I assume 0 or 1 because of original WHERE so COUNT(*) will suffice
        -- .. although, this implies an EXISTS could be used but let's keep it closer to OP post
        (
        SELECT ContentId, UniqueSubject, COUNT(*) AS NumberFound
        FROM @ATableVariable
        GROUP BY ContentId, UniqueSubject
        HAVING COUNT(*) > 0
        ) foo
        JOIN
        [dbo].[Contents] C ON C.ContentId = foo.ContentId AND C.UniqueSubject = foo.UniqueSubject
    

    Edit 2: and again with RANKING

    UPDATE
        C
    SET
        UniqueSubject + CAST(foo.Ranking - 1 AS NVARCHAR(10))
    FROM
        (
        SELECT
            ContentId, --not needed? UniqueSubject,
            ROW_NUMBER() OVER (PARTITION BY ContentId ORDER BY UniqueSubject) AS Ranking
        FROM
            @ATableVariable
        ) foo
    JOIN
        dbo.Contents C ON C.ContentId = foo.ContentId 
        /* not needed? AND C.UniqueSubject = foo.UniqueSubject */
    WHERE
    foo.Ranking > 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 247k
  • Answers 247k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Simply "create project from existing sources" in Anjuta. Anjuta's "project… May 13, 2026 at 8:39 am
  • Editorial Team
    Editorial Team added an answer The way the documentation worded it, it seemed like it… May 13, 2026 at 8:39 am
  • Editorial Team
    Editorial Team added an answer I suggest to create a .css file by Flex Style… May 13, 2026 at 8:39 am

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.