SQL Server 生成 Visual Studio 声称是错误的代码?
Posted
技术标签:
【中文标题】SQL Server 生成 Visual Studio 声称是错误的代码?【英文标题】:SQL Server generating code that Visual Studio claims to be an error? 【发布时间】:2014-04-28 15:27:42 【问题描述】:我正在与数据库同步的(维护不善的)VS2012 SqlServer 项目中有几十个 .sql 文件。我无法进行架构比较,因为 VS 抱怨存在构建错误。数据库将优先于 SQL 项目的内容,但我对一个 .sql 中的错误特别感到困惑:
USE [ePostRxIS] /* SQL70001: This statement is not recognized in this context */
GO
/****** Object: Table [SpyGlass].[Audit] Script Date: 01/16/2014 11:45:59 ******/
SET ANSI_NULLS ON /* SQL70001: This statement is not recognized in this context */
GO
SET QUOTED_IDENTIFIER ON /* SQL70001: This statement is not recognized in this context */
GO
SET ANSI_PADDING ON /* SQL70001: This statement is not recognized in this context */
GO
CREATE TABLE [SpyGlass].[Audit](
[AuditUID] [int] IDENTITY(1,1) NOT NULL,
[AuditTypeUID] [int] NOT NULL,
[SystemName] [varchar](25) NOT NULL,
[ApplicationName] [varchar](250) NOT NULL,
[Module] [varchar](250) NULL,
[Operation] [varchar](250) NULL,
[Description] [varchar](500) NOT NULL,
[AdditionalInfo] [varchar](max) NULL,
[CreatedDateTimeUTC] [datetime] NOT NULL,
[Username] [varchar](50) NULL,
[Synchronized] [bit] NOT NULL,
[Event] [varchar](250) NULL,
[EventTypeUID] [int] NULL,
[EntityTypeUID] [int] NULL,
[EntityUID] [int] NULL,
CONSTRAINT [PK_Audit] PRIMARY KEY CLUSTERED
(
[AuditUID] 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 ON /* SQL70001: This statement is not recognized in this context */
GO
/* SQL71508: The model already has an element that has the same name SpyGlass.Audit.AuditUID.MS_Description */
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Unique identifier and primary key.' , @level0type=N'SCHEMA',@level0name=N'SpyGlass', @level1type=N'TABLE',@level1name=N'Audit', @level2type=N'COLUMN',@level2name=N'AuditUID'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Foreign key into the AuditType table.' , @level0type=N'SCHEMA',@level0name=N'SpyGlass', @level1type=N'TABLE',@level1name=N'Audit', @level2type=N'COLUMN',@level2name=N'AuditTypeUID'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'The machine name running the application that generated the entry.' , @level0type=N'SCHEMA',@level0name=N'SpyGlass', @level1type=N'TABLE',@level1name=N'Audit', @level2type=N'COLUMN',@level2name=N'SystemName'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'The name of the application making the entry.' , @level0type=N'SCHEMA',@level0name=N'SpyGlass', @level1type=N'TABLE',@level1name=N'Audit', @level2type=N'COLUMN',@level2name=N'ApplicationName'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'The class name or "zone" of execution code.' , @level0type=N'SCHEMA',@level0name=N'SpyGlass', @level1type=N'TABLE',@level1name=N'Audit', @level2type=N'COLUMN',@level2name=N'Module'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'The web services operation that was invoked.' , @level0type=N'SCHEMA',@level0name=N'SpyGlass', @level1type=N'TABLE',@level1name=N'Audit', @level2type=N'COLUMN',@level2name=N'Operation'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'The content describing the reason for the entry.' , @level0type=N'SCHEMA',@level0name=N'SpyGlass', @level1type=N'TABLE',@level1name=N'Audit', @level2type=N'COLUMN',@level2name=N'Description'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Any additional information such as request/response XML, stack traces and so on.' , @level0type=N'SCHEMA',@level0name=N'SpyGlass', @level1type=N'TABLE',@level1name=N'Audit', @level2type=N'COLUMN',@level2name=N'AdditionalInfo'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'The UTC date and time the entry was created.' , @level0type=N'SCHEMA',@level0name=N'SpyGlass', @level1type=N'TABLE',@level1name=N'Audit', @level2type=N'COLUMN',@level2name=N'CreatedDateTimeUTC'
GO
ALTER TABLE [SpyGlass].[Audit] WITH CHECK ADD CONSTRAINT [FK_Audit_AuditType] FOREIGN KEY([AuditTypeUID])
REFERENCES [SpyGlass].[AuditType] ([AuditTypeUID])
GO
ALTER TABLE [SpyGlass].[Audit] CHECK CONSTRAINT [FK_Audit_AuditType]
GO
ALTER TABLE [SpyGlass].[Audit] WITH CHECK ADD CONSTRAINT [FK_Audit_EventType] FOREIGN KEY([EventTypeUID])
REFERENCES [SpyGlass].[EventType] ([EventTypeUID])
GO
ALTER TABLE [SpyGlass].[Audit] CHECK CONSTRAINT [FK_Audit_EventType]
GO
ALTER TABLE [SpyGlass].[Audit] ADD CONSTRAINT [DF_Audit_CreatedDateTime] DEFAULT (getutcdate()) FOR [CreatedDateTimeUTC]
GO
/* SQL71508: The model already has an element that has the same name SpyGlass.Audit.AuditUID.MS_Description */
EXECUTE sp_addextendedproperty @name = N'MS_Description', @value = N'Unique identifier and primary key.', @level0type = N'SCHEMA', @level0name = N'SpyGlass', @level1type = N'TABLE', @level1name = N'Audit', @level2type = N'COLUMN', @level2name = N'AuditUID';
GO
create index IDX_Audit_EntityHistory on SpyGlass.Audit (EntityTypeUID, EntityUID, Description, CreatedDateTimeUTC)
include (AuditUID, AuditTypeUID, SystemName, ApplicationName, Module, Operation, AdditionalInfo, Username, Synchronized, Event, EventTypeUID);
GO
CREATE NONCLUSTERED INDEX IDX_SpyGlass_Audit_History
ON [SpyGlass].[Audit] ([AuditTypeUID],[EntityTypeUID],[EntityUID],[CreatedDateTimeUTC])
INCLUDE (Description)
这是 .sql 的内容(添加了指示错误的 cmets),但如果我转到 SQL Server Management Studio,右键单击有问题的表(审核),将表编写为 > CREATE To,然后创建的脚本是正是 SQL 项目所拥有的。 VS 项目的目标平台设置为 SQL Server 2012,这是正确的目标环境。
我该怎么做才能让两者达成一致?我很想用数据库包含的内容重新创建项目,重新开始,但如果 VS 抱怨生成的代码,我该怎么办?如果我确实更正了 VS 项目以使其没有构建错误,那么架构比较不会不断告诉我存在差异吗?
【问题讨论】:
如果您所做的只是比较文件中的代码/字符错误之类的 give WinMerge a try. 这是一个出色的代码比较和对比工具,可以立即告诉您两个相似部分的差异在哪里代码是。 我们使用 Visual Studio 不仅因为它是我们熟悉的工具,还因为它使 TFS 管理更加容易。此外,由于我们的数据库是 SQL Server,它们通常可以很好地协同工作。 我也使用相同的版本,并且在使用他们脆弱的调试工具时遇到了重大挑战。一次通过检测一组问题,并且在不更正任何代码的情况下,另一次通过检测一组完全不同的代码问题。 【参考方案1】:SSDT 和 DB 项目只能识别“CREATE”语法,不能识别“USE”和“SET”命令。把这些从你的项目文件中拿出来,你应该会很好。
如果您最初尝试将整个数据库置于版本控制中,请考虑将其导入。您还可以考虑使用模式比较工具将数据库(作为源)与您的项目(作为目标)进行比较。
【讨论】:
我们想通过这个项目来维护和更改数据库。我们已经过了最初的导入。我已经取出了 USE 和 SET 命令,现在看起来还可以。我有更多的特殊性,但它们是针对我的情况的,所以我会与同事讨论解决这些问题。 我在博客中介绍了我们在 SSDT(和 DB 项目)方面的经验。我不知道这是否会有所帮助,但可能值得一试。 schottsql.blogspot.com/2013/10/all-ssdt-articles.html以上是关于SQL Server 生成 Visual Studio 声称是错误的代码?的主要内容,如果未能解决你的问题,请参考以下文章
Visual Studio SQL Server 2016 数据库项目和临时表
如何在 Visual Studio 2012 中使用 SQL Server Express 2008 部署 wpf 应用程序?
使用 Visual Studio 2012 和 SQL Server Management Studio 2012 运行 SQL Server CE 4 查询