How do I comment SQL code out in Microsoft Access?

情到浓时终转凉″ 提交于 2020-06-25 07:27:34

问题


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


回答1:


No. You cannot have any extraneous text in 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.




回答2:


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' ";



回答3:


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

' 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.

  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


来源:https://stackoverflow.com/questions/3152424/how-do-i-comment-sql-code-out-in-microsoft-access

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!