Solved

MSSQL Trigger On Same Table

Posted on 2014-02-20
6
296 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 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
shrink table after huge delete 2 28
SQL Database Restore 2008 R2 1 27
sql query display the latest row 10 54
Can I skip a node in XML? 9 36
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Detach & Attach 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.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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