SQL Server 2005 上的 IF NOT EXISTS 语句内的 DDL 未正确执行
我有一个相当复杂的数据库,需要部署到各种服务器上,这些服务器可能已经实现了数据库的现有部分,也可能没有。 为了解决这种意外情况,我设置了以下测试:
USE [testDB]
GO
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].[wcSites]') AND type in (N'U'))
begin
CREATE TABLE [dbo].[wcSites](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[siteCSS] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[masterTemplate] [int] NULL,
[errorPage] [int] NULL,
[homePage] [int] NULL,
[addressProduction] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[addressTest] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[routeHandler] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[publish] [bit] NOT NULL CONSTRAINT [DF_wcSites_publish] DEFAULT ((0)),
[publicAccess] [bit] NOT NULL CONSTRAINT [DF_wcSites_publicAccess] DEFAULT ((1)),
[siteAccessPermission] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[contentOwner] [int] NULL,
[navStyle] [int] NULL,
[incScripts] [int] NULL,
[boxW] [int] NULL,
[boxH] [int] NULL,
[columns] [int] NULL,
[rows] [int] NULL,
CONSTRAINT [PK_wcSites] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_wcSites_Unique_Address] UNIQUE NONCLUSTERED
(
[addressProduction] ASC,
[addressTest] 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
SET ANSI_PADDING OFF
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Ensure unique addresses in the addressProduction, addressTest fields' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'wcSites', @level2type=N'CONSTRAINT',@level2name=N'IX_wcSites_Unique_Address'
end
最终结果始终是:
Msg 102, Level 15, State 1, Line 32
Incorrect syntax near 'PRIMARY'.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'end'.
如果我测试 IF 语句,它会正常工作:
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[wcSites]') AND type in (N'U'))
begin
PRINT 'Table does not exist'
end
同样,当我测试 CREATE TABLE 脚本时。 但是当我将 CREATE TABLE 脚本放入 begin..end 块中时,它每次都会失败。 我遇到了全面的问题,几乎每个使用此方法的表都失败了。
I have a fairly complex database that needs to be deployed to a variety of servers which may or may not potentially have existing parts of the DB already implemented on it. To work around this contingency I have setup the following test:
USE [testDB]
GO
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].[wcSites]') AND type in (N'U'))
begin
CREATE TABLE [dbo].[wcSites](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[siteCSS] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[masterTemplate] [int] NULL,
[errorPage] [int] NULL,
[homePage] [int] NULL,
[addressProduction] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[addressTest] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[routeHandler] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[publish] [bit] NOT NULL CONSTRAINT [DF_wcSites_publish] DEFAULT ((0)),
[publicAccess] [bit] NOT NULL CONSTRAINT [DF_wcSites_publicAccess] DEFAULT ((1)),
[siteAccessPermission] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[contentOwner] [int] NULL,
[navStyle] [int] NULL,
[incScripts] [int] NULL,
[boxW] [int] NULL,
[boxH] [int] NULL,
[columns] [int] NULL,
[rows] [int] NULL,
CONSTRAINT [PK_wcSites] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_wcSites_Unique_Address] UNIQUE NONCLUSTERED
(
[addressProduction] ASC,
[addressTest] 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
SET ANSI_PADDING OFF
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Ensure unique addresses in the addressProduction, addressTest fields' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'wcSites', @level2type=N'CONSTRAINT',@level2name=N'IX_wcSites_Unique_Address'
end
And the end result is always:
Msg 102, Level 15, State 1, Line 32
Incorrect syntax near 'PRIMARY'.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'end'.
If I test the IF statement, it works correctly:
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[wcSites]') AND type in (N'U'))
begin
PRINT 'Table does not exist'
end
And likewise when I test the CREATE TABLE script. But when I put the CREATE TABLE script inside of the begin..end block it fails every time. And I have the problem across the board, almost every table that uses this method fails.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试删除 begin..end 块中的“go”语句,看看是否有帮助。
Try removing the 'go' statements within the begin..end block, see if that helps.
我对 SQL Server 语法有点模糊,但是 GO 语句不会尝试执行块吗? 如果用分号替换 go 会发生什么?
另外,Set Ansi_padding off 位于 If 块中,而 set Ansi_padding on 位于外部,这对我来说看起来很奇怪,您确定这是正确的吗?
I'm a bit hazy on the SQL Server syntax but doesn't the GO statement try to execute the Block? What happens if you replace the go with a semi-colon?
Also, the Set Ansi_padding off being in the If block while the set Ansi_padding on is outside looks odd to me, are you sure thats correct?