>>USE [Softex]; >>GO >>SET ANSI_NULLS ON; >>GO >>SET QUOTED_IDENTIFIER ON; >>GO >>/* Update Trigger '[dbo].[T_U_Catalogue]' for Table '[dbo].[Catalogue]' */ >> >>CREATE TRIGGER [dbo].[T_U_Catalogue] ON [dbo].[Catalogue] FOR UPDATE AS >> >>BEGIN >> DECLARE >> @row_count INT, >> @null_row_count INT, >> @error_number INT, >> @error_message VARCHAR(255) >> >> SELECT @row_count = @@rowcount >> IF @row_count = 0 >> RETURN >> >>/* When updating a row in child '[dbo].[Catalogue]' ,the Foreign Key must be Null or exist in Parent '[dbo].[cLookup]' */ >> >>IF UPDATE([FK_iOldCodeId]) >> BEGIN >> SELECT @null_row_count = >> ( >> SELECT COUNT(*) >> FROM inserted >> WHERE [FK_iOldCodeId] is null >> ) >> IF @null_row_count != @row_count >> IF ( >> SELECT COUNT(*) >> FROM [dbo].[cLookup] p, inserted i >> WHERE p.[iID] = i.[FK_iOldCodeId] >> ) >> != @row_count - @null_row_count >> BEGIN >> SELECT @error_number=30002, >> @error_message='Cannot update child in "[dbo].[Catalogue]".' >> GOTO error >> END >> END >> >>/* The Primary Key of '[dbo].[Catalogue]' cannot be modified if children exist in '[dbo].[Presentation]' */ >> >>IF UPDATE([iID]) >>BEGIN >> IF EXISTS ( >> SELECT 1 >> FROM [dbo].[Presentation] c, inserted i, deleted d >> WHERE c.[FK_iCatalogueId] = d.[iID] >> AND (i.[iID] != d.[iID]) >> ) >> BEGIN >> SELECT @error_number=30004, >> @error_message='Children exist in "[dbo].[Presentation]". Cannot modify Primary Key in "[dbo].[Catalogue]".' >> GOTO error >> END >>END >>RETURN >> >>/* Error Handling */ >>error: >> RAISERROR @error_number @error_message >> ROLLBACK TRANSACTION >>END >> >>GO >>>>We are approaching.
>RAISERROR (N'Error number %d message %s.', -- Message text. > 10, -- Severity, > 1, -- State, > @error_number, -- First argument. > @error_message); -- Second argument. >Thanks Borislav, it tells all!