Hi there,
Can you tell me if error handling is worth putting into this stored procedure?
-- **********************************************
-- Delete and create a new 'Truncate' function...
-- **********************************************
IF EXISTS(SELECT name
FROM sysobjects
WHERE name = 'truncate_description' AND
type = 'FN')
DROP FUNCTION truncate_description
GO
CREATE FUNCTION truncate_description(@description varchar(1000), @numchars int=500) RETURNS varchar(500)
BEGIN
DECLARE @newDescription varchar(500);
DECLARE @truncMessage varchar(33);
SET @truncMessage = '..Click on title to read article.';
IF (len(@description) >= @numchars)
IF (substring(@description, @numchars, 1) = '.')
SET @newDescription = @description;
ELSE
BEGIN
SET @newDescription = substring(@description, 1, @numchars - len(@truncMessage));
IF (charindex('.', @newDescription) > 0)
BEGIN
WHILE (substring(@newDescription, len(@newDescription), 1) <> '.')
BEGIN
SET @newDescription = substring(@newDescription, 1, len(@newDescription) - 1);
END
END
SET @newDescription = @newDescription + @truncMessage;
END
ELSE
SET @newDescription = @description;
RETURN @newDescription;
END
GO
-- *********************************************
-- Delete and create a new 'Truncate' trigger...
-- *********************************************
IF EXISTS(SELECT name
FROM sysobjects
WHERE name = 'tr_tblProfile_truncateDescription' AND
type = 'TR')
DROP TRIGGER tr_tblProfile_truncateDescription;
GO
CREATE TRIGGER tr_tblProfile_truncateDescription ON tbl_profile
AFTER INSERT, UPDATE AS
BEGIN
UPDATE tbl_profile
SET description = dbo.truncate_description(i.description, 500)
FROM tbl_profile p INNER JOIN inserted i on p.id=i.id
END
GO
-- ******************************************
-- Run the trigger on all existing records...
-- ******************************************
UPDATE tbl_Profile
SET description = description
GO
Many Thanks,