SqlException:'GO'附近的语法错误 [英] SqlException: Syntax Error Near 'GO'

查看:177
本文介绍了SqlException:'GO'附近的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 context.Database.ExecuteSqlCommand()通过DbContext发送SQL语句时遇到麻烦。

I am having trouble sending a SQL statement through a DbContext using context.Database.ExecuteSqlCommand().

我正在尝试执行

CREATE TABLE Phones([Id] [uniqueidentifier] NOT NULL PRIMARY KEY,
    [Number] [int],[PhoneTypeId] [int])
GO
ALTER TABLE [dbo].[Phones] ADD  CONSTRAINT [DF_Phones_Id]  
    DEFAULT (newid()) FOR [Id]
GO

此操作失败,并显示错误字符串

This fails with the error string

Incorrect syntax near the keyword 'ALTER'.
Incorrect syntax near 'GO'.

但是在SSMS中运行该确切语句不会出错吗?关于DbContext的默认约束,我需要解决的所有问题。我发现人们在使用约束并且没有将IsDbGenerated设置为true时遇到问题。我不确定该如何应用。

However running that exact statement in SSMS runs without errors? Any issues I need to resolve regarding the default constraint throught the DbContext. I have see problems with people using constraints and not having IsDbGenerated set to true. I am not sure how that would apply here though.

推荐答案

GO 不是SQL的一部分,因此无法使用ExecuteSqlCommand()执行。在使用Management Studio或命令行工具时,可以将GO视为一种分离批次的方法。相反,只需删除GO语句,就可以了。如果由于需要在单独的批处理中运行命令而出错,则只需为要运行的每个批处理调用一次ExecuteSqlCommand()。

GO is not a part of SQL, so it can't be executed with ExecuteSqlCommand(). Think of GO as a way to separate batches when using Management Studio or the command-line tools. Instead, just remove the GO statements and you should be fine. If you run into errors because you need to run your commands in separate batches, just call ExecuteSqlCommand() once for each batch you want to run.

这篇关于SqlException:'GO'附近的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆