Hi,I have a situation where I'd like to fire a trigger only when certain conditions are met for data that's inserted into it's parent table.Here is what I'm working with -The PropertyScenarioIndex table currently contains this data:PropertyID ScenarioType ScenarioID1732 financing 11732 financing 21732 financing 31732 income_expense 11732 income_expense 2The PropertyScenarioIndex table has a trigger on it (the one that needs to be made conditional) whose purpose is to automatically insert rows into another table called IncomeAndExpenseData to make entries easier for the user in that table. The primary keys in the PropertyScenarioIndex table are PropertyID, ScenarioType, ScenarioID and the primary keys in the IncomeExpenseData table are called PropertyID, ScenarioID, ItemID. Here is the trigger:CREATE TRIGGER addstandardIncomeAndExpenserows ON PropertyScenarioIndexAFTER INSERTASinsert into IncomeAndExpenseDataselectproperytID = t1.propertyID,scenarioID = t1.scenarioID,itemID = t2.itemID,itemvalue = null,monthitemvalue = nullfrom PropertyScenarioIndex t1cross join IncomeAndExpenseCodes t2wheret1.propertyID+t1.scenarioID in (select propertyID+scenarioID from Inserted)and t2.standarditem = 1
With the trigger as it is above, when the user tries to enter the following into PropertyScenarioIndex:PropertyID ScenarioType ScenarioID1732 income_expense 3an error message appears that says Violation of PRIMARY KEY constraint 'PK_IncomeExpenseData'. Cannot insert duplicate key in object 'IncomeExpenseData'.Is there some way to include code in the trigger that makes it fire only when the data inserted into the PropertyScenarioIndex table has a ScenarioType of 'income_expense'?Note: in the where statement of the trigger above I have also tried this:wheret1.propertyID+t1.scenarioID in (select propertyID+scenarioID from Inserted where scenariotype = 'income_expense')
but got the same error message as noted above.Does anyone know how to build some code into the trigger that only allows the trigger to fire when the data inserted into PropertyScenarioIndex.scenariotype = 'income_expense'?Thanks, Knot