Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MSSQL Trigger On Same Table

Posted on 2014-02-20
6
Medium Priority
?
300 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
[X]
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
  • 3
  • 3
6 Comments
 
LVL 70

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 70

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 70

Accepted Solution

by:
Scott Pletcher earned 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

636 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