下面是我的存储过程的drop和create脚本。在Database上执行这个脚本我遇到错误
Msg 102, Level 15, State 1, Line 18
Incorrect syntax near 'Date'.
Msg 105, Level 15, State 1, Line 35
Unclosed quotation mark after the character string '
END
'.
我尝试使用添加删除引号但没有效果。
任何人都可以告诉我为了成功执行脚本我必须做什么
/****** Object: StoredProcedure [dbo].[MY_PROC] Script Date: 9/12/2013 3:31:17 PM ******/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MY_PROC]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[MY_PROC]
GO
/****** Object: StoredProcedure [dbo].[MY_PROC] Script Date: 9/12/2013 3:31:17 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MY_PROC]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[MY_PROC]
(@P_Date DateTime=null)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @M_DATE_PARAM DATETIME
DECLARE @M_PREVIOUS_DATE DATETIME
DECLARE @M_TODAYS_DATE DATETIME
IF @P_Date IS NULL
SET @P_Date=GetDate()
PRINT 'Date passed as a parameter -' + CAST(@P_Date AS varchar(100))
BEGIN TRY
BEGIN TRANSACTION REIMBURSEMENT_TRN
BEGIN
-- my statements go here which do not cause any error
END
PRINT 'SUCCESS: Procedure Success'
COMMIT TRANSACTION ;
END TRY
BEGIN CATCH
PRINT 'ERROR: Procedure Fail'
PRINT ERROR_MESSAGE()
PRINT ERROR_LINE()
ROLLBACK TRANSACTION ;
END CATCH
PRINT 'Procedure Complete'
END
答案 0 :(得分:0)
PRINT需要字符串或 Unicode 字符串常量
尝试使用
PRINT N''Date passed as a parameter -''...
此外,您在END
程序之后错过了最终报价。
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[MY_PROC]
(@P_Date DateTime=null)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @M_DATE_PARAM DATETIME
DECLARE @M_PREVIOUS_DATE DATETIME
DECLARE @M_TODAYS_DATE DATETIME
IF @P_Date IS NULL
SET @P_Date=GetDate()
PRINT N''Date passed as a parameter -'' + CAST(@P_Date AS varchar(100))
BEGIN TRY
BEGIN TRANSACTION REIMBURSEMENT_TRN
BEGIN
-- my statements go here which do not cause any error
END
PRINT N''SUCCESS: Procedure Success''
COMMIT TRANSACTION ;
END TRY
BEGIN CATCH
PRINT N''ERROR: Procedure Fail''
PRINT ERROR_MESSAGE()
PRINT ERROR_LINE()
ROLLBACK TRANSACTION ;
END CATCH
PRINT N''Procedure Complete''
END'
答案 1 :(得分:0)
你应该用另一个引文转义单引号 试试这个:
PRINT ''Date passed as a parameter -'' + CAST(@P_Date AS varchar(100))
修改强>
顺便说一下,你应该在查询结尾处放置一个END
完整代码:
/****** Object: StoredProcedure [dbo].[MY_PROC] Script Date: 9/12/2013 3:31:17 PM ******/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MY_PROC]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[MY_PROC]
GO
/****** Object: StoredProcedure [dbo].[MY_PROC] Script Date: 9/12/2013 3:31:17 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MY_PROC]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql N'CREATE PROCEDURE [dbo].[MY_PROC]
(@P_Date DateTime=null)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @M_DATE_PARAM DATETIME
DECLARE @M_PREVIOUS_DATE DATETIME
DECLARE @M_TODAYS_DATE DATETIME
IF @P_Date IS NULL
SET @P_Date=GetDate()
PRINT ''Date passed as a parameter -'' + CAST(@P_Date AS varchar(100))
BEGIN TRY
BEGIN TRANSACTION REIMBURSEMENT_TRN
BEGIN
-- my statements go here which do not cause any error
END
PRINT ''SUCCESS: Procedure Success''
COMMIT TRANSACTION ;
END TRY
BEGIN CATCH
PRINT ''ERROR: Procedure Fail''
PRINT ERROR_MESSAGE()
PRINT ERROR_LINE()
ROLLBACK TRANSACTION ;
END CATCH
PRINT ''Procedure Complete''
END'
END