如何在 Microsoft Access 中注释掉 SQL 代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3152424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 06:44:00  来源:igfitidea点击:

How do I comment SQL code out in Microsoft Access?

sqlms-access

提问by l--''''''---------''''''''''''

Is it possible to comment code out in the SQL window in Microsoft Access?

是否可以在 Microsoft Access 的 SQL 窗口中注释掉代码?

采纳答案by MatthewMartin

No. You cannot have any extraneous text in Microsoft Access (JET-SQL).

不可以。在 Microsoft Access (JET-SQL) 中不能有任何无关的文本。

You can make some constraints ignored, e.g.,

您可以忽略一些约束,例如,

Where 
name = "joe"
OR
(state = "VA" AND 1=0)

But that technique is a rather limited way to hide existing SQL.

但该技术是隐藏现有 SQL 的一种相当有限的方法。

回答by mivk

As MathewMartin said, you can't. I use the following workaround:

正如马修马丁所说,你不能。我使用以下解决方法:

SELECT * FROM x
WHERE "-- your comment. This plain string is always true";

or

或者

SELECT * FROM x
WHERE y = 'something'
AND " -- z = 'something else' ";

回答by Dodecaphone

Access gives you the option of invoking queries from a VBA sub, which obviously can be commented to your heart's content:

Access 为您提供了从 VBA 子程序调用查询的选项,显然可以对您的内容进行评论:

' Ensure that the AddressCurrent in tblAddresses only has one item marked.
' Assume the latest.

strSQL = _
    "UPDATE tblAddresses " & _
    "SET AddressCurrent = 0 " & _
    "WHERE AddressCurrent = True "
' A comment can go in the middle if need be!
strSQL = strSQL & _
    "AND AddressNumber NOT IN " & _
         "(SELECT MAX (AddressNumber) " & _
         "FROM tblAddresses " & _
         "WHERE AddressCurrent = True);"

DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True

While having to run a macro that uses DoCmd might seem slightly tedious, it does compensate with other advantages; I've listed a few examples below.

虽然必须运行使用 DoCmd 的宏可能看起来有点乏味,但它确实弥补了其他优点;我在下面列出了几个例子。

  1. Possibility of dynamic scripts
  2. Ability to bind the execution of the SQL to form buttons and other controls
  3. Locked white space, making queries actually easier to read
  1. 动态脚本的可能性
  2. 能够将 SQL 的执行绑定到表单按钮和其他控件
  3. 锁定空白,使查询实际上更易于阅读