Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

MSSQL Trigger On Same Table

Posted on 2014-02-20
6
294 Views
Last Modified: 2014-02-20
Hello...

We have a table called "SY_WRKGRP", with a Primary Key on Field "WRKGRP_ID".  Within that table, there's a field called "NXT_ITEM_NO".

When a value is UPDATED on the "NXT_ITEM_NO" field, for *any* of the WRKGRP_ID's, we want/need the value of "NXT_ITEM_NO" to be updated with that same value...for all other WRKGRP_ID's.

We were having a issue with the Syntax, as it seems we're (somehow?) looping the originating WRKGRP_ID onto itself.  Please help...something's obviously amuck :-)

Thanks!...Mark
0
Comment
Question by:datatechcorp
  • 3
  • 3
6 Comments
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39875071
That doesn't really make sense to me.  Instead of updating every row, why not just put the NXT_ITEM_NO into a separate table?
0
 

Author Comment

by:datatechcorp
ID: 39875131
...because we cannot alter the Table Structure...we simply need "consistency" amongst the data field...once they are populated/updated (there's business logic behind all this).

I hope this helps...Thanks!...Mark
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39875142
UPDATE SY_WRKGRP
SET NXT_ITEM_NO = (
    SELECT TOP (1) NXT_ITEM_NO
    FROM inserted
    ORDER BY WRKGRP_ID DESC
    )


Not sure about the "onto itself" part ... updating the originally changed WRKGRP_ID row with the value from that row shouldn't really change the data.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:datatechcorp
ID: 39875176
Hi Scott...

It was suggested that we needed a 'NOCOUNT' instruction added in there somewhere...does that make sense?  Please let me know...Thanks!...Mark
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 39875308
Yes, that does indeed make sense, and it's quite true:

CREATE TRIGGER SY_WRKGRP__TRG
ON SY_WRKGRP
AFTER UPDATE --, INSERT
AS
SET NOCOUNT ON
UPDATE SY_WRKGRP
SET NXT_ITEM_NO = (
    SELECT TOP (1) NXT_ITEM_NO
    FROM inserted
    ORDER BY WRKGRP_ID DESC
    )
GO
0
 

Author Closing Comment

by:datatechcorp
ID: 39875330
Scott...thank you soooo much!  It's very appreciated!...Thanks!...Mark
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question