我试图在MS SQL Server上执行批处理(作为较大脚本的一部分),并且它返回错误列表:
Msg 156, Level 15, State 1, Line 32
Incorrect syntax near the keyword 'ALTER'.
Msg 102, Level 15, State 1, Line 33
Incorrect syntax near 'GO'.
等
这是批处理:
IF NOT EXISTS ( SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='ANS_247LIB_CIRCDESK_CONFIG_PROFILE')
BEGIN
DECLARE @SqlStr nvarchar(max)
SET @SqlStr= '
CREATE TABLE [dbo].[ANS_247LIB_CIRCDESK_CONFIG_PROFILE](
[CONFIG_ID] [int] IDENTITY(1,1) NOT NULL,
[SEARCH_BY_NAME] [nchar](1) NOT NULL,
[SEARCH_BY_BARCODE] [nchar](1) NOT NULL,
[SEARCH_BY_DOB] [nchar](1) NOT NULL,
[DISPLAY_USERID] [nchar](1) NOT NULL,
[DISPLAY_CHARGES] [nchar](1) NOT NULL,
[DISPLAY_RESERVATIONS] [nchar](1) NOT NULL,
[DISPLAY_DOB] [nchar](1) NOT NULL,
[DISPLAY_STATUS] [nchar](1) NOT NULL,
[DISPLAY_GENDER] [nchar](1) NOT NULL,
[DISPLAY_REFS] [nchar](1) NOT NULL,
[DISPLAY_LIBRARY] [nchar](1) NOT NULL,
[DISPLAY_EMAIL] [nchar](1) NOT NULL,
[DISPLAY_MOBILENO] [nchar](1) NOT NULL,
[FOREGROUND_COLOUR] [nvarchar](10) NOT NULL,
[BACKGROUND_COLOUR] [nvarchar](10) NOT NULL,
[BUTTON_COLOR] [nvarchar](10) NOT NULL,
[BOX_COLOR] [nvarchar](10) NOT NULL,
[TEXT_COLOR] [nvarchar](10) NOT NULL,
[PROFILE_ID] [int] NOT NULL,
CONSTRAINT [ANS_247LIB_CIRCDESK_CONFIG_PROFILE_pk] PRIMARY KEY CLUSTERED
(
[CONFIG_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ANS_247LIB_CIRCDESK_CONFIG_PROFILE] ADD CONSTRAINT [DF__ANS_247LI__SEARC__292D09F3] DEFAULT (''Y'') FOR [SEARCH_BY_NAME]
GO
ALTER TABLE [dbo].[ANS_247LIB_CIRCDESK_CONFIG_PROFILE] ADD CONSTRAINT [DF__ANS_247LI__SEARC__2A212E2C] DEFAULT (''Y'') FOR [SEARCH_BY_BARCODE]
GO
ALTER TABLE [dbo].[ANS_247LIB_CIRCDESK_CONFIG_PROFILE] ADD CONSTRAINT [DF__ANS_247LI__SEARC__2B155265] DEFAULT (''Y'') FOR [SEARCH_BY_DOB]
GO
ALTER TABLE [dbo].[ANS_247LIB_CIRCDESK_CONFIG_PROFILE] ADD CONSTRAINT [DF__ANS_247LI__DISPL__2C09769E] DEFAULT (''Y'') FOR [DISPLAY_USERID]
GO
ALTER TABLE [dbo].[ANS_247LIB_CIRCDESK_CONFIG_PROFILE] ADD CONSTRAINT [DF__ANS_247LI__DISPL__541767F8] DEFAULT (''Y'') FOR [DISPLAY_CHARGES]
GO
ALTER TABLE [dbo].[ANS_247LIB_CIRCDESK_CONFIG_PROFILE] ADD CONSTRAINT [DF__ANS_247LI__DISPL__2CFD9AD7] DEFAULT (''Y'') FOR [DISPLAY_DOB]
GO
ALTER TABLE [dbo].[ANS_247LIB_CIRCDESK_CONFIG_PROFILE] ADD CONSTRAINT [DF__ANS_247LI__DISPL__2DF1BF10] DEFAULT (''Y'') FOR [DISPLAY_STATUS]
GO
ALTER TABLE [dbo].[ANS_247LIB_CIRCDESK_CONFIG_PROFILE] ADD CONSTRAINT [DF__ANS_247LI__DISPL__2EE5E349] DEFAULT (''Y'') FOR [DISPLAY_GENDER]
GO
ALTER TABLE [dbo].[ANS_247LIB_CIRCDESK_CONFIG_PROFILE] ADD CONSTRAINT [DF__ANS_247LI__DISPL__2FDA0782] DEFAULT (''Y'') FOR [DISPLAY_REFS]
GO
ALTER TABLE [dbo].[ANS_247LIB_CIRCDESK_CONFIG_PROFILE] ADD CONSTRAINT [DF__ANS_247LI__DISPL__30CE2BBB] DEFAULT (''Y'') FOR [DISPLAY_LIBRARY]
GO
ALTER TABLE [dbo].[ANS_247LIB_CIRCDESK_CONFIG_PROFILE] ADD CONSTRAINT [DF__ANS_247LI__DISPL__31C24FF4] DEFAULT (''Y'') FOR [DISPLAY_EMAIL]
GO
ALTER TABLE [dbo].[ANS_247LIB_CIRCDESK_CONFIG_PROFILE] ADD CONSTRAINT [DF__ANS_247LI__DISPL__32B6742D] DEFAULT (''Y'') FOR [DISPLAY_MOBILENO]
GO
ALTER TABLE [dbo].[ANS_247LIB_CIRCDESK_CONFIG_PROFILE] ADD CONSTRAINT [DF__ANS_247LI__FOREG__33AA9866] DEFAULT (''#E0F7F7'') FOR [FOREGROUND_COLOUR]
GO
ALTER TABLE [dbo].[ANS_247LIB_CIRCDESK_CONFIG_PROFILE] ADD CONSTRAINT [DF__ANS_247LI__BACKG__349EBC9F] DEFAULT (''#00BCD4'') FOR [BACKGROUND_COLOUR]
GO
ALTER TABLE [dbo].[ANS_247LIB_CIRCDESK_CONFIG_PROFILE] ADD CONSTRAINT [DF_ANS_247LIB_CIRCDESK_CONFIG_PROFILE_BUTTON_COLOR] DEFAULT (''#00BCD4'') FOR [BUTTON_COLOR]
GO
ALTER TABLE [dbo].[ANS_247LIB_CIRCDESK_CONFIG_PROFILE] ADD CONSTRAINT [DF_ANS_247LIB_CIRCDESK_CONFIG_PROFILE_BOX_COLOR] DEFAULT (''#00BCD4'') FOR [BOX_COLOR]
GO
ALTER TABLE [dbo].[ANS_247LIB_CIRCDESK_CONFIG_PROFILE] ADD CONSTRAINT [DF_ANS_247LIB_CIRCDESK_CONFIG_PROFILE_TEXT_COLOR] DEFAULT (''#FFFFFF'') FOR [TEXT_COLOR]
GO
ALTER TABLE [dbo].[ANS_247LIB_CIRCDESK_CONFIG_PROFILE] WITH CHECK ADD CONSTRAINT [ANS_247LIB_CIRCDESK_CONFIG_PROFILE_ANS_PROFILE_PROFILE_ID_fk] FOREIGN KEY([PROFILE_ID])
REFERENCES [dbo].[ANS_PROFILE] ([PROFILE_ID])
GO
ALTER TABLE [dbo].[ANS_247LIB_CIRCDESK_CONFIG_PROFILE] CHECK CONSTRAINT [ANS_247LIB_CIRCDESK_CONFIG_PROFILE_ANS_PROFILE_PROFILE_ID_fk]
GO
'
EXEC (@SqlStr)
END
GO
我知道我可以一次执行一次这些语句,但是最好尽可能一次全部执行此脚本。
使用EXEC时,字符串中可以多次使用“ GO”吗?
答案 0 :(得分:2)
简而言之,Go是一个批处理分隔符。
您将必须删除动态SQL中的GO实例
答案 1 :(得分:1)
您可以在一个statement
命令中执行多个EXEC
,但是您不能在GO
中使用EXEC
(批量分隔符),因为它不属于{{1 }}:
SQL Server提供的命令不是Transact-SQL语句,而是 被sqlcmd和osql实用程序以及SQL Server识别 Management Studio代码编辑器。这些命令可以用来 促进批处理和脚本的可读性和执行。
GO 向SQL发出一批Transact-SQL语句的结束信号 服务器实用程序。