IF EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[Customer_Update_Words]'))
DROP TRIGGER [dbo].[Customer_Update_Words]
GO
CREATE TRIGGER [dbo].[Customer_Update_Words] ON [dbo].[Customer]
AFTER INSERT, UPDATE
AS
BEGIN
    SET NOCOUNT ON;

	IF UPDATE(FirstName) OR UPDATE(LastName) OR UPDATE(PhoneNumber) OR UPDATE(CellPhoneNumber) 
	BEGIN

		DELETE FROM dbo.CustomerWord
		FROM dbo.CustomerWord W
		INNER JOIN deleted D ON D.CustomerNumber=W.CustomerNumber
		WHERE W.ColumnID IN (1,2,5)

		;WITH T
		AS
		(
			SELECT 1 ColumnID, CustomerNumber, FirstName [Value] FROM inserted WITH (NOLOCK) WHERE FirstName IS NOT NULL AND FirstName<>'' AND ISNULL(CustomerAnonymized,0)=0 
			UNION ALL
			SELECT 2 ColumnID, CustomerNumber, LastName FROM inserted WITH (NOLOCK) WHERE LastName IS NOT NULL AND LastName<>'' AND ISNULL(CustomerAnonymized,0)=0 
			UNION ALL
			SELECT 5 ColumnID, CustomerNumber, REPLACE(PhoneNumber,' ','') FROM inserted  WITH (NOLOCK) WHERE PhoneNumber IS NOT NULL AND REPLACE(PhoneNumber,' ','')<>'' AND ISNULL(CustomerAnonymized,0)=0 
			UNION ALL
			SELECT 5 ColumnID, CustomerNumber, REPLACE(CellPhoneNumber,' ','') FROM inserted  WITH (NOLOCK) WHERE CellPhoneNumber IS NOT NULL AND REPLACE(CellPhoneNumber,' ','')<>'' AND ISNULL(CustomerAnonymized,0)=0 
		)
		INSERT INTO dbo.CustomerWord
		SELECT DISTINCT T.CustomerNumber, W.[Value], T.ColumnID 
		FROM T
		CROSS APPLY STRING_SPLIT(T.[Value], ' ') W
		WHERE RTRIM(W.Value) <> ''
		AND LEFT(RTRIM(W.Value),1) NOT IN ('#', '&', '?','<','/')
	END
END
GO
