在 SQL 中,是“FROM”在“删除自”中如果您打算使用“WHERE”,则可选;

发布于 2024-10-08 08:05:44 字数 545 浏览 5 评论 0原文

我是 SQL 新手。我们有一些代码应该可以在 SQL Server 2005/2008、Oracle 10 以及 Sybase 上运行。

我正在编写一个脚本来尝试找出给定存储过程修改(但不删除)哪些表,例如 insertupdatedelete

delete 令人费解 - 有时我会看到这样的语句:

delete phone_book where ... 

与:

delete from phone_book where ...

那么 ... 在这种情况下 from 关键字真的是可选的吗?这会导致任何问题吗?这只是一种糟糕的风格,还是无关紧要?

我还没有找到对 T-SQL 的引用,使 from 成为可选。我想这将统一我上面提到的所有 3 个供应商。

欢迎提出问题/评论/链接(或者是否欢迎?)。

I'm new to SQL. We have some code that should work on SQL Server 2005/2008, Oracle 10 as well as Sybase.

I was writing a script to try to figure out which tables a given stored procedure modifies (but does not drop), e.g insert, update and delete.

The delete one turned out being puzzling - sometimes I see statements like:

delete phone_book where ... 

as opposed to:

delete from phone_book where ...

So ... is the from keyword truly optional in this case? Does this cause any problems? Is it just a bad style, or does it not matter?

I have not found a reference to T-SQL that would make from optional. I suppose that this is what would unify all 3 vendors I mentioned above.

Questions/comments/links are welcomed (or is it welcome?).

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

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

发布评论

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

评论(5

为你鎻心 2024-10-15 08:05:44

在此位置,FROM 是可选的(SQL Server、OracleSybase)。

然而,存在细微的差别:例如,Oracle 允许为表名分配别名,而 SQL Server 则不允许;其他方面也有些不同。

另请注意,您的 FROM 示例与以下强制示例不同:

DELETE phone_book FROM some_table WHERE ...

At this place the FROM is optional (SQL Server, Oracle, Sybase).

However, there are subtle differences: Oracle for instance allows assigning an alias to the table name, where SQL Server doesn't; and other things are also a little bit different.

Also note that your FROM sample is differnet from the following where it is mandatory:

DELETE phone_book FROM some_table WHERE ...
最后的乘客 2024-10-15 08:05:44

简短的回答:Luceros 的回答是正确的:它是可选的

,我必须维护 sql 并在 sql-server 和 Oracle 之间进行调整。以下是一些规则:

  1. 手动编写脚本,不要使用生成的代码。
  2. 始终使用 INSERT INTO。
  3. 总是 DELETE——没有 FROM。
  4. 不要使用 " - 带引号的标识符。
  5. 删除所有 [ ] 和 dbo。(模式名称)
  6. 当您看到 DELETE ... FROM ... 时请注意
  7. ,当您看到 UPDATE ... FROM ... 时请注意
  8. ORACLE Select 语句需要可以在 DUAL 中使用的 from 子句

    1. 好的,您可以编写对象脚本并以标准方式编辑它们
      • 使用 [Current_DB] - 您不希望将测试数据库的引用纳入生产脚本
      • SET ANSI_NULLS ON - 决定一次使用哪些设置 - 不要打开和关闭
      • SET QUOTED_IDENTIFIER ON -- 带引号的标识符区分大小写。
    2. Oracle 要求 INSERT INTO。
    3. 这是我的个人风格,不要使用可选关键字,了解默认值
    4. 您必须引用一个标识符,如果您使用 ORACLES 保留关键字之一作为列名,我们就陷入了这个陷阱,从长远来看,最好在 sql-Server 端重命名该列。
    5. Oracle 不使用这些。
    6. Oracle 不支持此语法。
    7. Oracle 不支持此语法。

Short Answer: Luceros answer is correct: it is optional

I have to maintain sql and adapt it between sql-server and Oracle. Here are some rules:

  1. Write Scripts manually, don't use generated code.
  2. Always use INSERT INTO.
  3. Always DELETE -- without FROM.
  4. Do not use " - quoted identifier.
  5. Remove all [ ] and dbo. (Schema names)
  6. Attention when you see DELETE ... FROM ...
  7. Attention when you see UPDATE ... FROM ...
  8. ORACLE Select statements need a from clause you can use from DUAL

    1. OK you can script your objects and edit them in a standard way
      • USE [Current_DB] -- you don't want a reference to your test database go into production script
      • SET ANSI_NULLS ON -- decide once which settings to use -- don't switch on and off
      • SET QUOTED_IDENTIFIER ON -- quoted identifiers are case-sensitive.
    2. INSERT INTO is required by Oracle.
    3. That is my personal style don't use optional keyword, learn the defaults
    4. You have to quote an identifier, if you use one of ORACLES reserved keywords as column name, we entered that pitfall and in the long run it would have been better to rename the column on the sql-Server side.
    5. Oracle doesn't use these.
    6. Oracle doesn't support this syntax.
    7. Oracle doesn't support this syntax.
与君绝 2024-10-15 08:05:44

从 Microsoft SQL Server 文档来看,FROM 是可选的。

From the Microsoft SQL Server documentation, FROM is optional.

神仙妹妹 2024-10-15 08:05:44

SQL Server中,DELETE FROMFROM是可选的,没有FROMDELETE是不是SQL 标准,而DELETE FROMSQL 标准

我在 SQL ServerMySQL、<如下图所示:

数据库DELETE FROMDELETE
SQL Server可能可能
MySQL可能不可能
PostgreSQL可能< /strong>不可能
SQLite可能不可能

另外我还实验了INSERT INTO以及在 SQL ServerMySQLPostgreSQL上不使用 INTOINSERT SQLite 如下所示。

数据库INSERT INTOINSERT
SQL Server可能可能
MySQL可能可能
PostgreSQL可能不可能
SQLite<强>可能<强>不可能

In SQL Server, FROM of DELETE FROM is optional and DELETE without FROM is not SQL standard while DELETE FROM is SQL standard.

I experimented DELETE FROM and DELETE without FROM on SQL Server, MySQL, PostgreSQL and SQLite as shown below:

DatabaseDELETE FROMDELETE
SQL ServerPossiblePossible
MySQLPossibleImpossible
PostgreSQLPossibleImpossible
SQLitePossibleImpossible

In addition, I also experimented INSERT INTO and INSERT without INTO on SQL Server, MySQL, PostgreSQL and SQLite as shown below.

DatabaseINSERT INTOINSERT
SQL ServerPossiblePossible
MySQLPossiblePossible
PostgreSQLPossibleImpossible
SQLitePossibleImpossible
星星的轨迹 2024-10-15 08:05:44

在这三个 DBMS 中,fromdelete from 中是可选的,但根据 SQL 标准,它是强制性的。我总是使用 delete from 来简化 SQL 代码从一个 DBMS 到另一个 DBMS 的迁移。

from is optional in delete from in those three DBMSes but it is mandatory according to the SQL standard. I would always use delete from to ease the migration of SQL code from one DBMS to another.

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