Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
rajasekhar857
Constraint Violating Yak Guru
396 Posts |
Posted - 2010-08-23 : 09:17:53
|
[code]Create Trigger [dbo].TR_UPDATE_GROUPMASTER ON GROUPMASTERFor UpdateASDeclare @LOC_ID NUMERIC(20,0), @STATUS Numeric(20,0), @GROUP_ID Numeric(20,0)BEGIN set @STATUS = (SELECT STATUS FROM Inserted); set @GROUP_ID = (SELECT GROUP_ID FROM Deleted);DECLARE Locations CURSOR STATIC for SELECT DISTINCT LOCATION_ID FROM LOCATIONMASTER WHERE GROUP_ID = @GROUP_IDOPEN Locations FETCH NEXT FROM Locations INTO @LOC_ID WHILE @@FETCH_Status = 0 BEGIN IF (@STATUS = 1) BEGIN UPDATE EPRESCRIBECONSDETAILS SET REGISTRATION_STATUS = 1 WHERE LOCATION_ID =@LOC_ID UPDATE INTERFACECONNECTIONS SET CONNECTION_STATUS = 1 WHERE GROUP_ID = @GROUP_ID END ELSE BEGIN UPDATE EPRESCRIBECONSDETAILS SET REGISTRATION_STATUS = 3 WHERE LOCATION_ID = @LOC_ID UPDATE INTERFACECONNECTIONS SET CONNECTION_STATUS = 3 WHERE GROUP_ID= @GROUP_ID END FETCH NEXT FROM Locations INTO @LOC_ID END CLOSE Locations DEALLOCATE LocationsENDGO[/code]when i give queryupdate groupmaster set status=-1 where group_id>1502exception like thisMsg 512, Level 16, State 1, Procedure TR_UPDATE_GROUPMASTER, Line 8Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.The statement has been terminated.i know that = is failing for set @STATUS = (SELECT STATUS FROM Inserted); wat else needs to be changed for trigger to work |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-23 : 09:22:19
|
you're assuming that inserted and deleted tables will have only one row always which is not true. thats why the error------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-23 : 09:29:57
|
Actually you want thisCreate Trigger [dbo].TR_UPDATE_GROUPMASTER ON GROUPMASTERFor UpdateASBEGIN UPDATE eSET e.REGISTRATION_STATUS = CASE WHEN i.STATUS = 1 THEN 1 ELSE 3 ENDFROM EPRESCRIBECONSDETAILS eJOIN Deleted dON d.PK= l.PKJOIN INSERTED iON i.PK=d.PKJOIN LOCATIONMASTER lON l.GROUP_ID = d.GROUP_ID UPDATE eSET CONNECTION_STATUS = CASE WHEN i.STATUS = 1 THEN 1 ELSE 3 ENDFROM INTERFACECONNECTIONS eJOIN Deleted dON d.PK= l.PKJOIN INSERTED iON i.PK=d.PKENDGO ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
rajasekhar857
Constraint Violating Yak Guru
396 Posts |
Posted - 2010-08-23 : 13:01:49
|
ok Thanks |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-23 : 14:00:53
|
wc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
rajasekhar857
Constraint Violating Yak Guru
396 Posts |
Posted - 2010-08-24 : 01:50:20
|
Visakh, for pk what needs to be placed in my trigger like its a bit of confusing for me.can you clarify in this. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-24 : 07:43:52
|
pk means your primary key of table.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|