troubleshooting Question

More Trigger Help Needed

Avatar of wsturdev
wsturdevFlag for United States of America asked on
Microsoft SQL ServerMicrosoft SQL Server 2005
15 Comments2 Solutions281 ViewsLast Modified:
I have a table with some fields in it.  Four of them are:
RecID
StepNum
Product
Group

StepNum is a sequestial number.  For every Product, there will be 5 records, with StepNums 1 through 5.  Each Product belongs to a Group.

My Trigger is like this:
CREATETRIGGER [Propagate Updates Within Groups] ON [dbo].[myTable]
AFTER UPDATE
AS
BEGIN
       SET NOCOUNT ON;
       UPDATE dbo.myTable SET StepNum = (SELECT [StepNum] FROM INSERTED),
             OtherFields = (SELECT OtherFields FROM INSERT)
             WHERE (StepNum =  DELETED) AND (Group = DELETED)
END

This works when I change any of OtherFields in a single row and even when I want to change StepNum itself in a single row.

For Example:  UPDATE myTable SET StepNum = 20 WHERE StepNum = 5

BUT, how can I have the trigger work properly wen I want to work on multiple rows within a Profuct at the same time, as in:   UPDATE myTable SET StepNum = StepNum + 1 WHERE STepNum > 1 AND StepNum < 4.

I get an error:  Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 15 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 15 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros