我正在研究c#中的一个程序,它涉及在sql server中调用存储过程来创建表。问题是我无法在SQL服务器中创建过程,因为它一直给我语法错误,我仔细检查,似乎无法找到它的任何问题。
说明错误的语法不正确“TRY'靠近' Catch'我知道代码有点长,但我认为将它作为一个整体发布会更好。
存储过程应该通过调用sql server来创建表来执行它。
这是我的存储过程:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Nicholas
-- Create date: 11/01/2017
-- Description: Creating new tables for data archiving.
-- =============================================
CREATE PROCEDURE dbo.CreateTables
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
--use transaction when inserting data else you may end up with orphaned data and hard to debug issues later on
BEGIN TRANSACTION
--add your CREATE queries one after the other below
/****** Object: Table [dbo].[PromotionEvent_New] Script Date: 03/10/2017 2:29:57 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PromotionEvent_New]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[PromotionEvent_New](
[EventCode] [varchar](8) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Description] [varchar](250) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[ChDescription] [nvarchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[AdditionalInfo] [varchar](250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[ChAdditionalInfo] [nvarchar](250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[EventTypeCode] [varchar](5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[StartDate] [datetime] NOT NULL,
[EndDate] [datetime] NOT NULL,
[StartTime] [datetime] NULL,
[EndTime] [datetime] NULL,
[CategoryId] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[BenefitSelection] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[NoOfBenefitSelection] [int] NULL,
[LimitedVoucherAllotment] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[SplitAllotment] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[LimitedPermission] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[AttachMemberID] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[DRSPreferenceFlag] [varchar](4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[PreferenceValue] [varchar](15) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[LimitedRedemption] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[MaxRedemptionQty] [int] NULL,
[QtyPerRedemption] [int] NULL,
[RedemptionPeriod] [int] NULL,
[PeriodType] [char](5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[CalendarDate] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[CreateBy] [nvarchar](128) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[CreateDate] [datetime] NOT NULL,
[Status] [char](2) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[CancelReason] [varchar](250) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[DailyAllotment] [char](1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[LastChangeBy] [nvarchar](128) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[LastChangeDate] [datetime] NOT NULL,
[ImageFile] [image] NULL,
[RecordVersion] [timestamp] NOT NULL,
[ImageFileName] [varchar](200) COLLATE Latin1_General_CI_AS NULL,
[DRSReport] [varchar](5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[CXLParameterCode] [varchar](8) COLLATE Latin1_General_CI_AS NULL,
[CRMCode] [varchar](36) COLLATE Latin1_General_CI_AS NULL,
[NonCRMProfile] [char](2) COLLATE Latin1_General_CI_AS NULL,
[OTTarget] [bit] NULL,
[evsProgID] [varchar](10) COLLATE Latin1_General_CI_AS NULL,
CONSTRAINT [PK_PromotionEvent_New] PRIMARY KEY CLUSTERED
(
[EventCode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
END
GO
SET ANSI_PADDING OFF
GO
ALTER AUTHORIZATION ON [dbo].[PromotionEvent_New] TO SCHEMA OWNER
GO
GRANT DELETE ON [dbo].[PromotionEvent_New] TO [APPRole] AS [dbo]
GO
GRANT INSERT ON [dbo].[PromotionEvent_New] TO [APPRole] AS [dbo]
GO
GRANT SELECT ON [dbo].[PromotionEvent_New] TO [APPRole] AS [dbo]
GO
GRANT UPDATE ON [dbo].[PromotionEvent_New] TO [APPRole] AS [dbo]
GO
SET ANSI_PADDING ON
GO
/****** Object: Index [_dta_index_PromotionEvent_18_1013578649__K6_K1_K28_K7_K8_New] Script Date: 03/10/2017 2:29:57 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[PromotionEvent_New]') AND name = N'_dta_index_PromotionEvent_18_1013578649__K6_K1_K28_K7_K8_New')
CREATE NONCLUSTERED INDEX [_dta_index_PromotionEvent_18_1013578649__K6_K1_K28_K7_K8_New] ON [dbo].[PromotionEvent_New]
(
[EventTypeCode] ASC,
[EventCode] ASC,
[Status] ASC,
[StartDate] ASC,
[EndDate] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object: Index [idx_PromotionEvent_3_New] Script Date: 03/10/2017 2:29:57 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[PromotionEvent_New]') AND name = N'idx_PromotionEvent_3_New')
CREATE NONCLUSTERED INDEX [idx_PromotionEvent_3_New] ON [dbo].[PromotionEvent_New]
(
[StartDate] ASC,
[EndDate] ASC,
[StartTime] ASC,
[EndTime] ASC,
[LimitedVoucherAllotment] ASC,
[LimitedPermission] ASC,
[AttachMemberID] ASC,
[Status] ASC,
[DailyAllotment] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object: Index [IX_PromotionEvent_1_New] Script Date: 03/10/2017 2:29:57 PM ******/
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[PromotionEvent_New]') AND name = N'IX_PromotionEvent_1_New')
CREATE NONCLUSTERED INDEX [IX_PromotionEvent_1_New] ON [dbo].[PromotionEvent_New]
(
[StartDate] ASC,
[EndDate] ASC,
[Status] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DF_PromotionEvent_DRSPreferenceFlag_New]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[PromotionEvent_New] ADD CONSTRAINT [DF_PromotionEvent_DRSPreferenceFlag_New] DEFAULT ('') FOR [DRSPreferenceFlag]
END
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DF_PromotionEvent_PreferenceValue_New]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[PromotionEvent_New] ADD CONSTRAINT [DF_PromotionEvent_PreferenceValue_New] DEFAULT ('') FOR [PreferenceValue]
END
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DF_PromotionEvent_QtyPerRedemption_New]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[PromotionEvent_New] ADD CONSTRAINT [DF_PromotionEvent_QtyPerRedemption_New] DEFAULT ('') FOR [QtyPerRedemption]
END
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DF_PromotionEvent_RedemptionPeriod_New]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[PromotionEvent_New] ADD CONSTRAINT [DF_PromotionEvent_RedemptionPeriod_New] DEFAULT ('') FOR [RedemptionPeriod]
END
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DF__Promotion__OTTar__6DA491D6_New]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[PromotionEvent_New] ADD CONSTRAINT [DF__Promotion__OTTar__6DA491D6_New] DEFAULT ((0)) FOR [OTTarget]
END
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DF__Promotion__evsPr__6E98B60F_New]') AND type = 'D')
BEGIN
ALTER TABLE [dbo].[PromotionEvent_New] ADD CONSTRAINT [DF__Promotion__evsPr__6E98B60F_New] DEFAULT ('GCVS_Promo') FOR [evsProgID]
END
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_PromotionEvent_EventCategory_New]') AND parent_object_id = OBJECT_ID(N'[dbo].[PromotionEvent_New]'))
ALTER TABLE [dbo].[PromotionEvent_New] WITH NOCHECK ADD CONSTRAINT [FK_PromotionEvent_EventCategory_New] FOREIGN KEY([CategoryId])
REFERENCES [dbo].[EventCategory] ([CategoryId])
GO
IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_PromotionEvent_EventCategory_New]') AND parent_object_id = OBJECT_ID(N'[dbo].[PromotionEvent_New]'))
ALTER TABLE [dbo].[PromotionEvent_New] CHECK CONSTRAINT [FK_PromotionEvent_EventCategory_New]
GO
/****** Object: Trigger [dbo].[tg_D_PROMOTIONEVENT_New] Script Date: 03/10/2017 2:29:57 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[tg_D_PROMOTIONEVENT_New]'))
EXEC dbo.sp_executesql @statement = N'CREATE TRIGGER [dbo].[tg_D_PROMOTIONEVENT_New] ON [dbo].[PromotionEvent_New] FOR DELETE AS
INSERT INTO AUDITPROMOTIONEVENT( [AuditType], [EventCode], [Description], [ChDescription],
[AdditionalInfo], [ChAdditionalInfo], [EventTypeCode], [StartDate], [EndDate],
[StartTime], [EndTime], [CategoryId], [BenefitSelection], [NoOfBenefitSelection],
[LimitedVoucherAllotment], [SplitAllotment], [LimitedPermission], [AttachMemberID],
[DRSPreferenceFlag], [PreferenceValue], [LimitedRedemption], [MaxRedemptionQty], [QtyPerRedemption],
[RedemptionPeriod], [PeriodType], [CalendarDate], [CreateBy], [CreateDate], [Status], [CancelReason],
[LastChangeBy], [LastChangeDate], [CRMCode],[NonCRMProfile] )
SELECT ''D'' ,rec.[EventCode], rec.[Description], rec.[ChDescription],
rec.[AdditionalInfo], rec.[ChAdditionalInfo], rec.[EventTypeCode], rec.[StartDate], rec.[EndDate],
rec.[StartTime], rec.[EndTime], rec.[CategoryId], rec.[BenefitSelection], rec.[NoOfBenefitSelection],
rec.[LimitedVoucherAllotment], rec.[SplitAllotment], rec.[LimitedPermission], rec.[AttachMemberID],
rec.[DRSPreferenceFlag], rec.[PreferenceValue], rec.[LimitedRedemption], rec.[MaxRedemptionQty], rec.[QtyPerRedemption],
rec.[RedemptionPeriod], rec.[PeriodType], rec.[CalendarDate], rec.[CreateBy], rec.[CreateDate], rec.[Status], rec.[CancelReason],
rec.[LastChangeBy], rec.[LastChangeDate], rec.[CRMCode],rec.[NonCRMProfile]
from deleted rec'
GO
ALTER TABLE [dbo].[PromotionEvent_New] ENABLE TRIGGER [tg_D_PROMOTIONEVENT_New]
GO
/****** Object: Trigger [dbo].[tg_I_PROMOTIONEVENT_New] Script Date: 03/10/2017 2:29:57 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[tg_I_PROMOTIONEVENT_New]'))
EXEC dbo.sp_executesql @statement = N'CREATE TRIGGER [dbo].[tg_I_PROMOTIONEVENT_New] ON [dbo].[PromotionEvent_New] FOR INSERT AS
INSERT INTO AUDITPROMOTIONEVENT( [AuditType], [EventCode], [Description], [ChDescription],
[AdditionalInfo], [ChAdditionalInfo], [EventTypeCode], [StartDate], [EndDate],
[StartTime], [EndTime], [CategoryId], [BenefitSelection], [NoOfBenefitSelection],
[LimitedVoucherAllotment], [SplitAllotment], [LimitedPermission], [AttachMemberID],
[DRSPreferenceFlag], [PreferenceValue], [LimitedRedemption], [MaxRedemptionQty], [QtyPerRedemption],
[RedemptionPeriod], [PeriodType], [CalendarDate], [CreateBy], [CreateDate], [Status], [CancelReason],
[LastChangeBy], [LastChangeDate], [CRMCode],[NonCRMProfile] )
SELECT ''I'' , rec.[EventCode], rec.[Description], rec.[ChDescription],
rec.[AdditionalInfo], rec.[ChAdditionalInfo], rec.[EventTypeCode], rec.[StartDate], rec.[EndDate],
rec.[StartTime], rec.[EndTime], rec.[CategoryId], rec.[BenefitSelection], rec.[NoOfBenefitSelection],
rec.[LimitedVoucherAllotment], rec.[SplitAllotment], rec.[LimitedPermission], rec.[AttachMemberID],
rec.[DRSPreferenceFlag], rec.[PreferenceValue], rec.[LimitedRedemption], rec.[MaxRedemptionQty], rec.[QtyPerRedemption],
rec.[RedemptionPeriod], rec.[PeriodType], rec.[CalendarDate], rec.[CreateBy], rec.[CreateDate], rec.[Status], rec.[CancelReason],
rec.[LastChangeBy], rec.[LastChangeDate], rec.[CRMCode],rec.[NonCRMProfile]
from inserted rec'
GO
ALTER TABLE [dbo].[PromotionEvent_New] ENABLE TRIGGER [tg_I_PROMOTIONEVENT_New]
GO
/****** Object: Trigger [dbo].[tg_U_PROMOTIONEVENT_New] Script Date: 03/10/2017 2:29:57 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[tg_U_PROMOTIONEVENT_New]'))
EXEC dbo.sp_executesql @statement = N'CREATE TRIGGER [dbo].[tg_U_PROMOTIONEVENT_New] ON [dbo].[PromotionEvent_New] FOR UPDATE AS
INSERT INTO AUDITPROMOTIONEVENT
([AuditType], [EventCode], [Description], [ChDescription],
[AdditionalInfo], [ChAdditionalInfo], [EventTypeCode], [StartDate], [EndDate],
[StartTime], [EndTime], [CategoryId], [BenefitSelection], [NoOfBenefitSelection],
[LimitedVoucherAllotment], [SplitAllotment], [LimitedPermission], [AttachMemberID],
[DRSPreferenceFlag], [PreferenceValue], [LimitedRedemption], [MaxRedemptionQty], [QtyPerRedemption],
[RedemptionPeriod], [PeriodType], [CalendarDate], [CreateBy], [CreateDate], [Status], [CancelReason],
[LastChangeBy], [LastChangeDate], [CRMCode],[NonCRMProfile] )
SELECT ''U'' , rec.[EventCode], rec.[Description], rec.[ChDescription],
rec.[AdditionalInfo], rec.[ChAdditionalInfo], rec.[EventTypeCode], rec.[StartDate], rec.[EndDate],
rec.[StartTime], rec.[EndTime], rec.[CategoryId], rec.[BenefitSelection], rec.[NoOfBenefitSelection],
rec.[LimitedVoucherAllotment], rec.[SplitAllotment], rec.[LimitedPermission], rec.[AttachMemberID],
rec.[DRSPreferenceFlag], rec.[PreferenceValue], rec.[LimitedRedemption], rec.[MaxRedemptionQty], rec.[QtyPerRedemption],
rec.[RedemptionPeriod], rec.[PeriodType], rec.[CalendarDate], rec.[CreateBy], rec.[CreateDate], rec.[Status], rec.[CancelReason],
rec.[LastChangeBy], rec.[LastChangeDate], rec.[CRMCode],rec.[NonCRMProfile]
from deleted rec'
GO
ALTER TABLE [dbo].[PromotionEvent_New] ENABLE TRIGGER [tg_U_PROMOTIONEVENT_New]
END
--add more of your create tables queries below
-- finally commit transaction
COMMIT TRANSACTION
END TRY
BEGIN CATCH
DECLARE @errorDetails NVARCHAR(MAX);
set @errorDetails = 'Error ' + CONVERT(VARCHAR(50), ERROR_NUMBER()) +
', Severity ' + CONVERT(VARCHAR(5), ERROR_SEVERITY()) +
', State ' + CONVERT(VARCHAR(5), ERROR_STATE()) +
', Line ' + CONVERT(VARCHAR(5), ERROR_LINE());
--roll back the transaction
IF XACT_STATE() <> 0
BEGIN
ROLLBACK TRANSACTION
END
--you can log the above error message and/or re-throw the error so your C# code will see an error
--but do this only after rolling back
END CATCH;
END
GO
答案 0 :(得分:0)
SQL Server实用程序将GO解释为应发送的信号 当前批处理的Transact-SQL语句到SQL的一个实例 服务器。当前批次的陈述由所有陈述组成 自上次GO开始,或自特别会议开始以来或 如果这是第一个GO脚本。 +
答案 1 :(得分:0)
您不能在存储过程,函数或触发器中使用GO。 通常,GO会终止程序声明。