Sybase:“go”附近的语法不正确在“如果存在”中堵塞

发布于 2024-12-25 14:22:13 字数 344 浏览 4 评论 0原文

这是我的 sql 语句,

IF EXISTS (select 1 from sysobjects where name = 'PNL_VALUE_ESTIMATE')
  drop table dbo.PNL_VALUE_ESTIMATE
go

isql 会显示此错误消息

Msg 102, Level 15, State 1:
Server 'DB_SERVER', Line 3:
Incorrect syntax near 'go'.

,但该 sql 语句对我来说看起来是正确的。怎么了?

Sybase版本为15

This is my sql statement

IF EXISTS (select 1 from sysobjects where name = 'PNL_VALUE_ESTIMATE')
  drop table dbo.PNL_VALUE_ESTIMATE
go

isql bails out with this error message

Msg 102, Level 15, State 1:
Server 'DB_SERVER', Line 3:
Incorrect syntax near 'go'.

But the sql statement look correct to me. What's wrong?

Sybase version is 15

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暮凉 2025-01-01 14:22:13

试试这个:

IF EXISTS (select 1 from sysobjects where name = 'PNL_VALUE_ESTIMATE')
  drop table dbo.PNL_VALUE_ESTIMATE

go

或者这个:

IF EXISTS (select 1 from sysobjects where name = 'PNL_VALUE_ESTIMATE')
BEGIN
  drop table dbo.PNL_VALUE_ESTIMATE
END

go

或者这个:

IF EXISTS (select 1 from sysobjects where name = 'PNL_VALUE_ESTIMATE')
BEGIN
  select 1
END

go

有效果吗?

Try this:

IF EXISTS (select 1 from sysobjects where name = 'PNL_VALUE_ESTIMATE')
  drop table dbo.PNL_VALUE_ESTIMATE

go

or this:

IF EXISTS (select 1 from sysobjects where name = 'PNL_VALUE_ESTIMATE')
BEGIN
  drop table dbo.PNL_VALUE_ESTIMATE
END

go

or this:

IF EXISTS (select 1 from sysobjects where name = 'PNL_VALUE_ESTIMATE')
BEGIN
  select 1
END

go

Does any work?

如歌彻婉言 2025-01-01 14:22:13

GO不是T-SQL的关键字,而是编辑器的关键字。

SMSS(以及其他)将其用作发送到数据库服务器的命令批次之间的“划分”。在存储过程甚至脚本文件中执行它是行不通的。

编辑:也许它适用于 SyBase,但我认为在这种情况下它需要大写。

GO is not a keyword of T-SQL, but of the editor.

SMSS (between others) uses it as 'division' between batches of commands it sends to the database server. Executing it inside a stored procedure, or even a script file, won't work.

edit: Maybe it works with SyBase, but I think it'll need to be uppercase in that case.

我三岁 2025-01-01 14:22:13

从文档中可以看出,GO 语句是您正在使用的编辑器的命令,而不是 SQL 本身:

GO 不是 Transact-SQL 语句;这是一个被识别的命令
sqlcmd 和 osql 实用程序以及 SQL Server Management Studio 代码
编辑器。

也就是说,Sybase 也是一个支持 GO 语句的编辑器。

我也遇到过同样的问题,但是是使用 SQL Management Studio。问题是编辑器不支持某些语句周围的混合换行符类型 - GO 就是其中之一。例如,在 Management Studio 中,只允许 Windows 风格的换行符 (CR + LF),如果我使用 Linux 格式 (LF),它将给出与上面完全相同的错误。

Notepad++(我使用的)等文本编辑器可以选择默认使用的行尾字符类型(Windows、Linux、Mac (CR))。

尝试检查语句中使用了哪些换行符,看看是否可以解决问题。

From the documentation, the GO statement is a command of the editor you're using, not SQL itself:

GO is not a Transact-SQL statement; it is a command recognized by the
sqlcmd and osql utilities and SQL Server Management Studio Code
editor.

That said - Sybase is also an editor that supports the GO statement.

I've had the same problem, but with SQL Management Studio. The issue is that the editor does not support mixed-newline types around certain statements - GO being one of them. In Management Studio, for example, only Windows-style newlines (CR + LF) are allowed and if I were to use the Linux format (LF), it will give the exact same error as yours above.

Text-editors such as Notepad++ (what I use) have an option for what type of End-of-Line characters you use by default (Windows, Linux, Mac (CR)).

Try checking what newline character(s) are being used in your statements to see if that can fix the problem.

别再吹冷风 2025-01-01 14:22:13

对象引用不应该有

dbo..PNL_VALUE_ESTIMATE

因为您没有给出数据库名称,并且如果您包含 obj 所有者,则需要 .. 错过数据库名称?

我也会在 true 部分中执行:

EXEC('DROP TABLE dbo..PNL_VALUE_ESTIMATE')

,因为 DROP TABLE 始终会被编译,如果表不存在,您仍然会失败。

你还需要dbo吗?如果您的 sql 始终以 dbo 身份运行,则将其省略。

Shouldn't the object reference have

dbo..PNL_VALUE_ESTIMATE

because you haven't given a database name, and if you include the obj owner you need .. to miss the db name?

I'd go:

EXEC('DROP TABLE dbo..PNL_VALUE_ESTIMATE')

in the true part as well, because DROP TABLE is always compiled, and if the table isn't there you'll still have a failure.

Do you even need dbo? If your sql always runs as dbo just leave it out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文