J
Joe
I am building a small helper application to create a table, stored
procedures and triggers. I need to output the SQL formatted instead
of all together. For instance, here is an Update Trigger that I have
generated,
CREATE TRIGGER updTrips ON dbo.Trips AFTER UPDATE AS IF @@ROWCOUNT = 0
RETURN INSERT INTO [dbo].[Audit_Changes] ([PrimaryID],
[ColumnChanged], [TableChanged], [OldValue], [NewValue], [Username],
[DTChanged], [ActionType]) SELECT i.TripID, CASE col# WHEN 2 THEN
'TripDate' WHEN 3 THEN 'StartTime' WHEN 4 THEN 'EndTime' ELSE '?' END,
'Trips', CASE col# WHEN 2 THEN CAST(d.TripDate AS VARCHAR(25)) WHEN 3
THEN d.StartTime WHEN 4 THEN d.EndTime ELSE '?' END, CASE col# WHEN 2
THEN CAST(i.TripDate AS VARCHAR(25)) WHEN 3 THEN i.StartTime WHEN 4
THEN i.EndTime ELSE '?' END, SUSER_SNAME(), GETDATE(), 'U' FROM
inserted i INNER JOIN deleted d ON i.TripID = d.TripID CROSS JOIN
( SELECT 2 AS col# UNION ALL SELECT 3 AS col# UNION ALL SELECT 4 AS
col# ) AS col#s WHERE ISNULL(CASE col# WHEN 2 THEN CAST(i.TripDate AS
VARCHAR(25)) WHEN 3 THEN i.StartTime WHEN 4 THEN i.EndTime ELSE '?'
END, '') <> ISNULL(CASE col# WHEN 2 THEN CAST(d.TripDate AS VARCHAR
(25)) WHEN 3 THEN d.StartTime WHEN 4 THEN d.EndTime ELSE '?' END, '')
Which is very, very messy... I would like to figure out how to format
the SQL so it looks something like this,
CREATE TRIGGER updTrips
ON dbo.Trips
AFTER UPDATE
AS
IF @@ROWCOUNT = 0
RETURN
INSERT INTO [dbo].[Audit_Changes] ([PrimaryID], [ColumnChanged],
[TableChanged], [OldValue], [NewValue], [Username], [DTChanged],
[ActionType])
SELECT i.TripID,
CASE col#
WHEN 2 THEN 'TripDate'
WHEN 3 THEN 'StartTime'
WHEN 4 THEN 'EndTime'
WHEN 5 THEN 'Duration'
WHEN 6 THEN 'RLU'
WHEN 7 THEN 'TripPlace'
WHEN 8 THEN 'TripPurpose'
ELSE '?' END,
'Trips',
CASE col#
WHEN 2 THEN CAST(d.TripDate AS VARCHAR(25))
WHEN 3 THEN d.StartTime
WHEN 4 THEN d.EndTime
WHEN 5 THEN d.Duration
WHEN 6 THEN d.RLU
WHEN 7 THEN d.TripPlace
WHEN 8 THEN d.TripPurpose
ELSE '?' END,
CASE col#
WHEN 2 THEN CAST(i.TripDate AS VARCHAR(25))
WHEN 3 THEN i.StartTime
WHEN 4 THEN i.EndTime
WHEN 5 THEN i.Duration
WHEN 6 THEN i.RLU
WHEN 7 THEN i.TripPlace
WHEN 8 THEN i.TripPurpose
ELSE '?' END,
SUSER_SNAME(),
GETDATE(),
'U'
FROM inserted i
INNER JOIN deleted d ON i.TripID = d.TripID
CROSS JOIN (
SELECT 2 AS col# UNION ALL
SELECT 3 AS col# UNION ALL
SELECT 4 AS col# UNION ALL
SELECT 5 AS col# UNION ALL
SELECT 6 AS col# UNION ALL
SELECT 7 AS col# UNION ALL
SELECT 8 AS col# )
AS col#s
WHERE ISNULL(CASE col#
WHEN 2 THEN CAST(i.TripDate AS VARCHAR(25))
WHEN 3 THEN i.StartTime
WHEN 4 THEN i.EndTime
WHEN 5 THEN i.Duration
WHEN 6 THEN i.RLU
WHEN 7 THEN i.TripPlace
WHEN 8 THEN i.TripPurpose
ELSE '?' END, '') <>
ISNULL(
CASE col#
WHEN 2 THEN CAST(d.TripDate AS VARCHAR(25))
WHEN 3 THEN d.StartTime
WHEN 4 THEN d.EndTime
WHEN 5 THEN d.Duration
WHEN 6 THEN d.RLU
WHEN 7 THEN d.TripPlace
WHEN 8 THEN d.TripPurpose
ELSE '?' END, '')
I have tried vbCrLf, vbNewLine, etc... and nothing seems to work. Any
ideas?
Thanks,
Drew
procedures and triggers. I need to output the SQL formatted instead
of all together. For instance, here is an Update Trigger that I have
generated,
CREATE TRIGGER updTrips ON dbo.Trips AFTER UPDATE AS IF @@ROWCOUNT = 0
RETURN INSERT INTO [dbo].[Audit_Changes] ([PrimaryID],
[ColumnChanged], [TableChanged], [OldValue], [NewValue], [Username],
[DTChanged], [ActionType]) SELECT i.TripID, CASE col# WHEN 2 THEN
'TripDate' WHEN 3 THEN 'StartTime' WHEN 4 THEN 'EndTime' ELSE '?' END,
'Trips', CASE col# WHEN 2 THEN CAST(d.TripDate AS VARCHAR(25)) WHEN 3
THEN d.StartTime WHEN 4 THEN d.EndTime ELSE '?' END, CASE col# WHEN 2
THEN CAST(i.TripDate AS VARCHAR(25)) WHEN 3 THEN i.StartTime WHEN 4
THEN i.EndTime ELSE '?' END, SUSER_SNAME(), GETDATE(), 'U' FROM
inserted i INNER JOIN deleted d ON i.TripID = d.TripID CROSS JOIN
( SELECT 2 AS col# UNION ALL SELECT 3 AS col# UNION ALL SELECT 4 AS
col# ) AS col#s WHERE ISNULL(CASE col# WHEN 2 THEN CAST(i.TripDate AS
VARCHAR(25)) WHEN 3 THEN i.StartTime WHEN 4 THEN i.EndTime ELSE '?'
END, '') <> ISNULL(CASE col# WHEN 2 THEN CAST(d.TripDate AS VARCHAR
(25)) WHEN 3 THEN d.StartTime WHEN 4 THEN d.EndTime ELSE '?' END, '')
Which is very, very messy... I would like to figure out how to format
the SQL so it looks something like this,
CREATE TRIGGER updTrips
ON dbo.Trips
AFTER UPDATE
AS
IF @@ROWCOUNT = 0
RETURN
INSERT INTO [dbo].[Audit_Changes] ([PrimaryID], [ColumnChanged],
[TableChanged], [OldValue], [NewValue], [Username], [DTChanged],
[ActionType])
SELECT i.TripID,
CASE col#
WHEN 2 THEN 'TripDate'
WHEN 3 THEN 'StartTime'
WHEN 4 THEN 'EndTime'
WHEN 5 THEN 'Duration'
WHEN 6 THEN 'RLU'
WHEN 7 THEN 'TripPlace'
WHEN 8 THEN 'TripPurpose'
ELSE '?' END,
'Trips',
CASE col#
WHEN 2 THEN CAST(d.TripDate AS VARCHAR(25))
WHEN 3 THEN d.StartTime
WHEN 4 THEN d.EndTime
WHEN 5 THEN d.Duration
WHEN 6 THEN d.RLU
WHEN 7 THEN d.TripPlace
WHEN 8 THEN d.TripPurpose
ELSE '?' END,
CASE col#
WHEN 2 THEN CAST(i.TripDate AS VARCHAR(25))
WHEN 3 THEN i.StartTime
WHEN 4 THEN i.EndTime
WHEN 5 THEN i.Duration
WHEN 6 THEN i.RLU
WHEN 7 THEN i.TripPlace
WHEN 8 THEN i.TripPurpose
ELSE '?' END,
SUSER_SNAME(),
GETDATE(),
'U'
FROM inserted i
INNER JOIN deleted d ON i.TripID = d.TripID
CROSS JOIN (
SELECT 2 AS col# UNION ALL
SELECT 3 AS col# UNION ALL
SELECT 4 AS col# UNION ALL
SELECT 5 AS col# UNION ALL
SELECT 6 AS col# UNION ALL
SELECT 7 AS col# UNION ALL
SELECT 8 AS col# )
AS col#s
WHERE ISNULL(CASE col#
WHEN 2 THEN CAST(i.TripDate AS VARCHAR(25))
WHEN 3 THEN i.StartTime
WHEN 4 THEN i.EndTime
WHEN 5 THEN i.Duration
WHEN 6 THEN i.RLU
WHEN 7 THEN i.TripPlace
WHEN 8 THEN i.TripPurpose
ELSE '?' END, '') <>
ISNULL(
CASE col#
WHEN 2 THEN CAST(d.TripDate AS VARCHAR(25))
WHEN 3 THEN d.StartTime
WHEN 4 THEN d.EndTime
WHEN 5 THEN d.Duration
WHEN 6 THEN d.RLU
WHEN 7 THEN d.TripPlace
WHEN 8 THEN d.TripPurpose
ELSE '?' END, '')
I have tried vbCrLf, vbNewLine, etc... and nothing seems to work. Any
ideas?
Thanks,
Drew