trigger causing update errors | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

trigger causing update errors

Hello,<br /><br />I recently put a trigger on one of my tables (proj). It is working fine, but today I tried to run this statement from query analyzer: <br /><br />update proj set fiscalyear = ‘2004’<br /><br />and got this error:<br /><br />Server: Msg 512, Level 16, State 1, Procedure DupData, Line 5<br />Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, &lt;, &lt;= , &gt;, &gt;= or when the subquery is used as an expression.<br />The statement has been terminated.<br /><br />I did some research, and found that it could be my trigger causing the problems. Do you know how I can fix this trigger to work correctly? Here is the trigger code:<br /><br />CREATE TRIGGER [DupData] ON [dbo].[Proj]<br />AFTER UPDATE<br />AS<br />declare @ProjID varchar(<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ />,@CustomerID varchar(12),@DivisionID varchar(12),@FiscalYear varchar(4),@Abbreviation varchar(1),@DateTimeLastChanged varchar(20)<br />set @ProjID = (select projid from inserted)<br />set @CustomerID = (select CustomerID from inserted)<br />set @DivisionID = (select DivisionID from inserted)<br />set @FiscalYear = (select FiscalYear from inserted)<br />set @Abbreviation = (select Abbreviation from inserted)<br />set @DateTimeLastChanged = (select DateTimeLastChanged from inserted)<br />IF (UPDATE(CustomerID))<br />BEGIN<br />IF EXISTS(select * from [Case] where [Case].[CaseID] = @ProjID)<br />BEGIN<br />update [Case] set [Case].[CustomerID] = @CustomerID where [Case].[CaseID] = @ProjID<br />END<br />IF EXISTS(select * from [Job] where [Job].[JobID] = @ProjID)<br />BEGIN<br />update [Job] set [Job].[CustomerID] = @CustomerID where [Job].[JobID] = @ProjID<br />END<br />IF EXISTS(select * from [Lead] where [Lead].[LeadID] = @ProjID)<br />BEGIN<br />update [Lead] set [Lead].[CustomerID] = @CustomerID where [Lead].[LeadID] = @ProjID<br />END<br />IF EXISTS(select * from [Proposal] where [Proposal].[ProposalID] = @ProjID)<br />BEGIN<br />update [Proposal] set [Proposal].[CustomerID] = @CustomerID where [Proposal].[ProposalID] = @ProjID<br />END<br />END<br />IF (UPDATE(DivisionID))<br />BEGIN<br />IF EXISTS(select * from [Case] where [Case].[CaseID] = @ProjID)<br />BEGIN<br />update [Case] set [Case].[DivisionID] = @DivisionID where [Case].[CaseID] = @ProjID<br />END<br />IF EXISTS(select * from [Job] where [Job].[JobID] = @ProjID)<br />BEGIN<br />update [Job] set [Job].[DivisionID] = @DivisionID where [Job].[JobID] = @ProjID<br />END<br />IF EXISTS(select * from [Lead] where [Lead].[LeadID] = @ProjID)<br />BEGIN<br />update [Lead] set [Lead].[DivisionID] = @DivisionID where [Lead].[LeadID] = @ProjID<br />END<br />IF EXISTS(select * from [Proposal] where [Proposal].[ProposalID] = @ProjID)<br />BEGIN<br />update [Proposal] set [Proposal].[DivisionID] = @DivisionID where [Proposal].[ProposalID] = @ProjID<br />END<br />END<br />IF (UPDATE(FiscalYear))<br />BEGIN<br />IF EXISTS(select * from [Case] where [Case].[CaseID] = @ProjID)<br />BEGIN<br />update [Case] set [Case].[FiscalYear] = @FiscalYear where [Case].[CaseID] = @ProjID<br />END<br />IF EXISTS(select * from [Job] where [Job].[JobID] = @ProjID)<br />BEGIN<br />update [Job] set [Job].[FiscalYear] = @FiscalYear where [Job].[JobID] = @ProjID<br />END<br />IF EXISTS(select * from [Lead] where [Lead].[LeadID] = @ProjID)<br />BEGIN<br />update [Lead] set [Lead].[FiscalYear] = @FiscalYear where [Lead].[LeadID] = @ProjID<br />END<br />IF EXISTS(select * from [Proposal] where [Proposal].[ProposalID] = @ProjID)<br />BEGIN<br />update [Proposal] set [Proposal].[FiscalYear] = @FiscalYear where [Proposal].[ProposalID] = @ProjID<br />END<br />END<br />IF (UPDATE(Abbreviation))<br />BEGIN<br />IF EXISTS(select * from [Case] where [Case].[CaseID] = @ProjID)<br />BEGIN<br />update [Case] set [Case].[Abbreviation] = @Abbreviation where [Case].[CaseID] = @ProjID<br />END<br />IF EXISTS(select * from [Job] where [Job].[JobID] = @ProjID)<br />BEGIN<br />update [Job] set [Job].[Abbreviation] = @Abbreviation where [Job].[JobID] = @ProjID<br />END<br />IF EXISTS(select * from [Lead] where [Lead].[LeadID] = @ProjID)<br />BEGIN<br />update [Lead] set [Lead].[Abbreviation] = @Abbreviation where [Lead].[LeadID] = @ProjID<br />END<br />IF EXISTS(select * from [Proposal] where [Proposal].[ProposalID] = @ProjID)<br />BEGIN<br />update [Proposal] set [Proposal].[Abbreviation] = @Abbreviation where [Proposal].[ProposalID] = @ProjID<br />END<br />END<br />IF (UPDATE(DateTimeLastChanged))<br />BEGIN<br />IF EXISTS(select * from [Case] where [Case].[CaseID] = @ProjID)<br />BEGIN<br />update [Case] set [Case].[DateTimeLastChanged] = @DateTimeLastChanged where [Case].[CaseID] = @ProjID<br />END<br />IF EXISTS(select * from [Job] where [Job].[JobID] = @ProjID)<br />BEGIN<br />update [Job] set [Job].[DateTimeLastChanged] = @DateTimeLastChanged where [Job].[JobID] = @ProjID<br />END<br />IF EXISTS(select * from [Lead] where [Lead].[LeadID] = @ProjID)<br />BEGIN<br />update [Lead] set [Lead].[DateTimeLastChanged] = @DateTimeLastChanged where [Lead].[LeadID] = @ProjID<br />END<br />IF EXISTS(select * from [Proposal] where [Proposal].[ProposalID] = @ProjID)<br />BEGIN<br />update [Proposal] set [Proposal].[DateTimeLastChanged] = @DateTimeLastChanged where [Proposal].[ProposalID] = @ProjID<br />END<br />END<br /><br /><br /><br />Thanks for you help,<br /><br />Ben
You haven’t anticipated updates affecting multiple records. You get this error when there are multiple records in the "inserted" table. You can only SET a variable to a value from the inserted table if not more than 1 record was affected. Since the tables that you need to modify all have a foreign key referring to ProjID, you can simply use a join: UPDATE <othertable> SET <othertable>.<otherfield> = inserted.<field>
FROM <othertable> INNER JOIN inserted on <othertable>.<id> = inserted.projid
Thanks a lot. That solved my problem.
]]>