CREATE VIEW [dbo].[pal_vCampaignDetail]
AS
SELECT        C.recId, C.campaignTitle, C.campaignBody, C.campaignType AS campaignTypeId, C.isRecurring, C.interval AS intervalId, C.messageType, C.lastSent, C.nextSend, C.createdBy, C.dateSaved, C.dateUpdated, C.isActive, C.isDraft, 
                         C.contactSourceSurface, C.contactNumberField, C.contactNameField, C.filterField, C.filterValue, I.interval, T.campaignType, ISNULL(U.name + ' ' + U.surname, '') AS createdByName
FROM            dbo.pal_Campaign AS C INNER JOIN
                         dbo.pal_CampaignInterval AS I ON I.recId = C.interval INNER JOIN
                         dbo.pal_CampaignType AS T ON T.recId = C.campaignType LEFT OUTER JOIN
                         dbo.pal_vUserShared AS U ON U.recId = C.createdBy
WHERE        (T.campaignType <> 'Ad Hoc') OR
                         (T.campaignType = 'Ad Hoc') AND (C.isDraft = 1)
