我有以下SQL语句,因此我可以使用sp_executesql
使用if not exists编写视图创建脚本,但由于我上一个where语句中的单引号为1,因此该语句给出了错误。有没有办法解决?
IF NOT EXISTS (SELECT * FROM SYS.objects WHERE NAME = 'vw_JeopardyAlertDetails' AND TYPE = 'V')
EXEC sp_executesql @statement = N'CREATE VIEW [dbo].[vw_JeopardyAlertDetails]
AS
SELECT Main.TicketNumber, TS.TicketStateDesc, Main.ApptEnd, ISNULL(C.FirstName, '') AS FirstName, ISNULL(C.LastName, '') AS LastName, ISNULL(Main.CustomerID, '')
AS CustomerID, Main.ApptID, Main.ID, Main.TicketType
FROM (SELECT s.TicketState, s.TicketID AS ID, s.ApptEnd, dbo.Ticket.TicketNumber, dbo.Ticket.TimeOpened, dbo.Ticket.CreatedBy, dbo.Ticket.ReportedBy,
dbo.Ticket.ModifiedBy, dbo.Ticket.ChangedBy, dbo.Ticket.Priority, dbo.Ticket.ServingArea, dbo.Ticket.StructureLink, dbo.Ticket.CustomerID,
dbo.Ticket.TicketCategory, dbo.Ticket.TicketCode, s.ApptStart, s.TicketType, s.CanReschedule, ISNULL(s.ID, 0) AS ApptID
FROM dbo.Schedule AS s INNER JOIN
dbo.Ticket ON s.TicketID = dbo.Ticket.ID
WHERE (s.TicketState IN
(SELECT DISTINCT TicketState
FROM dbo.AlertJeopardyTicketState
WHERE (IsJeopardyState = '1'))) AND (s.ApptEnd >= CONVERT(DATETIME, CONVERT(VARCHAR(10), GETDATE(), 111) + ' ' + dbo.GetJeopardyStartTime()))
AND (s.ApptEnd <= GETDATE())) AS Main LEFT OUTER JOIN
dbo.Customer AS C ON Main.CustomerID = C.ID LEFT OUTER JOIN
dbo.TicketStatus AS TS ON Main.TicketState = TS.ID
GO' ;
ELSE PRINT N'vw_JeopardyAlertDetails ALREADY EXISTS'
GO
答案 0 :(得分:4)