I have to automatically script the triggers of my tables in a database. I am getting a syntax error for the trigger script when creating the stored procedure text. Copying the trigger definition text into a query window in SQL Server shows that the definition does not contain CRLF characters in the script most of the definition is on the comment line.
Is there anyway to add new line characters into the trigger definition text. I get the trigger definition using this query:
SELECT trig.name AS "@TriggerName", REPLACE(OBJECT_DEFINITION(trig.object_id), '''', '''''') AS "@TrigDefinition"
FROM sys.triggers trig
WHERE trig.type = 'TR'
The text of the trigger defintion of a text trigger looks like this:
-- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description: <Description,,> -- =============================================
CREATE TRIGGER TestTrigger2 ON BundleProduct AFTER DELETE,UPDATE AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for trigger here select * from Edging; END
How can I add new line characters to the trigger script so that I can add it to a stored procedure that is created from a query?
UPDATE I tried adding newline characters (CHAR(13) + CHAR(10) to the trigger definition using the REPLACE string function:
DECLARE @NewLine varchar(2) = CHAR(13) + CHAR(10);
REPLACE(TrigDefinition, '--', @NewLine + '--')
So before every comment I add new line characters but it does not work.