Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Update constraint on multiple fields
Message
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2000
Miscellaneous
Thread ID:
01275199
Message ID:
01275432
Views:
7
>>But what is the logic... Would something like this work?
>>
>>
>>CREATE TRIGGER CheckPK ON [Product]
>>FOR UPDATE
>>AS
>>DECLARE @countFk int
>>SELECT @countFk = SELECT count(fk_product) from Traveler where fk_product = insert.pk)
>>
>>IF @countFk > 0
>>	AND (fk_engine != insert.fk_engine
>>	OR fk_stage != insert.fk_stage
>>	OR fk_part != insert.fk_part)
>>IF EXISTS ( SELECT * FROM inserted i JOIN DELETED d ON d.pk = i.pk 
				WHERE d.fk_engine <> i.fk_engine 
					OR d.fk_stage <> i.fk_stage 
					OR d.fk_part <> i.fk_part)


>>
>>
>
>Margaret,
>
>I think you should check the previous value (e.g. from deleted) and then may be your logic against inserted table, though I'm not sure what exactly you're checking.
>
>In other words, it should check
>
>SELECT @countFk = SELECT count(fk_product) from Traveler where fk_product = deleted.pk)
>
>and then act accordingly. Also do you want to check if you changed fk_engine, etc. fields? If yes, then join deleted with inserted tables based on the PK.

Combining yours and Sergey's idea it would be:
 if exists (select * from Traveler where fk_product = deleted.PK) and 
  EXISTS ( SELECT * FROM inserted i JOIN DELETED d ON d.pk = i.pk 
				WHERE d.fk_engine <> i.fk_engine 
					OR d.fk_stage <> i.fk_stage 
					OR d.fk_part <> i.fk_part)

  begin
    Print ...
    Print ....
    rollback 
  end
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform