问题
I have created an insert/update trigger that is designed to update information in a different table based on the data being inserted. The last thing the trigger does (or is supposed to do) is remove all data from a target table with conditions that may have changed during the insert portions of the trigger.
Everything appears to be working with the trigger except the final DELETE
statement. It is executing the DELETE
statement, but not following any of the conditions in the where clause. It simply deletes everything in the table.
I have even tried changing the NOT IN
in the where clause to IN
, and it still does the same. I have isolated the DELETE
statement and tested outside the trigger and it works fine (using the same variables and subqueries).
Am I missing something with the behavior of a trigger?
Here comes the code:
ALTER TRIGGER [dbo].[cust_trgr_profile_attribute]
ON [dbo].[port_module_instance_setting]
AFTER INSERT, UPDATE
AS
DECLARE @ModuleId INT=449,
@MatchGroupModSetting VARCHAR(50) = 'AttributeGroup',
@FilterGroupModSetting VARCHAR(50) = 'FilterAttributeGroup',
@MatchAttributes TABLE (attribute_id INT),
@FilterAttributes TABLE (attribute_id INT)
INSERT INTO @MatchAttributes
SELECT DISTINCT camatch.attribute_id
FROM inserted I
JOIN core_attribute camatch ON I.value = CONVERT(VARCHAR(10), camatch.attribute_group_id)
JOIN port_module_instance pmi ON I.module_instance_id = pmi.module_instance_id
AND pmi.module_id=@ModuleId
WHERE I.name like @MatchGroupModSetting+'_'
INSERT INTO @FilterAttributes
SELECT DISTINCT cafilter.attribute_id
FROM inserted I
JOIN core_attribute cafilter ON I.value = CONVERT(VARCHAR(10), cafilter.attribute_group_id)
JOIN port_module_instance pmi ON I.module_instance_id = pmi.module_instance_id
AND pmi.module_id=@ModuleId
WHERE I.name=@FilterGroupModSetting
IF ((SELECT COUNT(*) FROM @MatchAttributes) > 0 OR (SELECT COUNT(*) FROM @FilterAttributes) > 0)
BEGIN
IF (SELECT COUNT(*) FROM @MatchAttributes) > 0
BEGIN
UPDATE cpa
SET cpa.[required]=0
FROM cust_profile_attribute cpa
JOIN @MatchAttributes ma ON cpa.attribute_id = ma.attribute_id
END
IF (SELECT COUNT(*) FROM @FilterAttributes) > 0
BEGIN
UPDATE cpa
SET cpa.[required]=0
FROM cust_profile_attribute cpa
JOIN @FilterAttributes fa ON cpa.attribute_id=fa.attribute_id
END
DELETE FROM cust_profile_attribute
WHERE attribute_id NOT IN (SELECT distinct ca.attribute_id
FROM core_attribute ca
JOIN port_module_instance_setting inst ON CONVERT(VARCHAR(10),ca.attribute_group_id) = inst.value
JOIN port_module_instance modinst ON inst.module_instance_id = modinst.module_instance_id
AND modinst.module_id = @ModuleId
WHERE inst.name like @MatchGroupModSetting + '_'
OR inst.name like @FilterGroupModSetting)
END
回答1:
I have found a flaw in my fundamental logic of how the trigger works. I have now refined my understanding of what is going on and able to articulate more information for others to help. Rather than try to completely transform the original post, I thought it better to post the new info here.
The basic idea is that the port_module_instance_setting
table stores a string in the that represents a setting (in this specific case, the conditions are set so that the value
field will always be a number). What I am trying to accomplish is that when the value
field is updated in one of these specific "settings", everything in the cust_profile_attribute
table that is referenced by the old value
gets deleted. In this context, the value
field of port_module_instance_setting
is a numeric varchar value that directly references the attribute_group_id
of core_attribute
. Please don't comment on best practices regarding referencing tables using different data types, as I have no control over the actual table structure :)
Everything in the trigger functions properly, except the DELETE
statement at the end. It isn't doing anything. Any ideas on why it isn't deleting the attributes I want it to?
I have changed the DELETE
statement as follows to reference the pre-updated value
field.
DELETE FROM cust_profile_attribute
WHERE attribute_id IN(SELECT ISNULL(attribute_id,-1) FROM deleted d
JOIN core_attribute ca ON ca.attribute_group_id= CONVERT(INT,d.value))
来源:https://stackoverflow.com/questions/11767725/delete-statement-issues-within-a-trigger-definition