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 7732131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:39:17+00:00 2026-06-01T06:39:17+00:00

I need to create a Trigger that fires when a child record (Codes) is

  • 0

I need to create a Trigger that fires when a child record (Codes) is added, updated or deleted. The Trigger stuffs a string of comma separated Code values from all child records (Codes) into a single field in the parent record (Projects) of the added, updated or deleted child record.

I am stuck on writing a correct query to retrieve the Code values from just those child records that are the children of a single parent record.

-- Create the test tables
CREATE TABLE projects (
  ProjectId varchar(16) PRIMARY KEY,
  ProjectName varchar(100),
  Codestring nvarchar(100)
)
GO
CREATE TABLE prcodes (
  CodeId varchar(16) PRIMARY KEY,
  Code varchar (4),
  ProjectId varchar(16)
)
GO
-- Add sample data to tables: Two projects records, one with 3 child records, the other with 2.
INSERT INTO projects
(ProjectId, ProjectName)
SELECT '101','Smith' UNION ALL
SELECT '102','Jones'
GO
INSERT INTO prcodes
(CodeId, Code, ProjectId)
SELECT 'A1','Blue', '101' UNION ALL
SELECT 'A2','Pink', '101' UNION ALL
SELECT 'A3','Gray', '101' UNION ALL
SELECT 'A4','Blue', '102' UNION ALL
SELECT 'A5','Gray', '102'
GO

I am stuck on how to create a correct Update query.
Can you help fix this query?

-- Partially working, but stuffs all values, not just values from chile (prcodes) records of parent (projects)
UPDATE proj
SET
proj.Codestring = (SELECT STUFF((SELECT ',' + prc.Code 
FROM projects proj INNER JOIN prcodes prc ON proj.ProjectId = prc.ProjectId
ORDER BY 1 ASC FOR XML PATH('')),1, 1, ''))

The result I get for the Codestring field in Projects is:

    ProjectId   ProjectName Codestring
    101     Smith       Blue,Blue,Gray,Gray,Pink
    ...

But the result I need for the Codestring field in Projects is:

    ProjectId   ProjectName Codestring
    101     Smith       Blue,Pink,Gray
    ...

Here is my start on the Trigger. The Update query, above, will be added to this Trigger. Can you help me complete the Trigger creation query?

CREATE TRIGGER Update_Codestring ON prcodes
AFTER INSERT, UPDATE, DELETE
AS
WITH CTE AS (
  select ProjectId from inserted
  union
  select ProjectId from deleted
)
  • 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-06-01T06:39:18+00:00Added an answer on June 1, 2026 at 6:39 am

    The following trigger will perform as you want.

    CREATE TRIGGER Update_Codestring ON prcodes
    AFTER INSERT, UPDATE, DELETE
    AS
    UPDATE projects
    SET Codestring = (SELECT STUFF((SELECT ',' + prc.Code 
        FROM projects proj INNER JOIN prcodes prc ON proj.ProjectId = prc.ProjectId
        WHERE proj.ProjectId = projects.ProjectId
        ORDER BY 1 ASC FOR XML PATH('')),1, 1, ''))
    where ProjectId in (SELECT ProjectId FROM inserted
                        UNION
                        SELECT ProjectId FROM deleted)
    

    What you were missing in your original update statement was:

    WHERE proj.ProjectId = projects.ProjectId – This will filter the subquery to only the project that is being updated. projects with no alias comes from the update statement so as update is applied against each row in projects only the current project row being updated.

    WHERE ProjectId IN (SELECT ProjectId FROM inserted UNION SELECT ProjectId FROM deleted) – This will filter the update to affect only the rows with changed children.

    Also you can simplify the update statement since it doesn’t need the projects table included twice:

    CREATE TRIGGER Update_Codestring ON prcodes
    AFTER INSERT, UPDATE, DELETE
    AS
    UPDATE projects
    SET Codestring = (SELECT STUFF((SELECT ',' + prc.Code 
        FROM prcodes prc
        WHERE prc.ProjectId = projects.ProjectId
        ORDER BY 1 ASC FOR XML PATH('')),1, 1, ''))
    WHERE ProjectId IN (SELECT ProjectId FROM inserted
                        UNION
                        SELECT ProjectId FROM deleted)
    

    Finally do you really need to store the Codestring on your Projects table? It’s something that can easily be recalculated in a query at anytime or even put into a view. That was you don’t have to worry about having to store the extra data and have a trigger to maintain it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a trigger that will create a record in another table.
I am renewing a record. I need to create a trigger that will automatically
I need to create a trigger that writes changes in a shadow table. I
Below is trigger that I need to create but It is not getting created.Please
I'm working with wordpress and need to create a trigger that updates a table
I need to create Trigger for delete action which backsup all fields old value
Someone told me that I need to use a delimiter in my trigger. I'm
I need to change this trigger to point to the column that just got
I need a SQL trigger that would zero pad a cell whenever its inserted
I have a trigger that gets inserts and updates to a view. I need

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.