Solved

MSSQL Trigger On Same Table

Posted on 2014-02-20
6
291 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:ScottPletcher
Comment Utility
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
Comment Utility
...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:ScottPletcher
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:datatechcorp
Comment Utility
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:
ScottPletcher earned 500 total points
Comment Utility
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
Comment Utility
Scott...thank you soooo much!  It's very appreciated!...Thanks!...Mark
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now