失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > sql常见语法_常见SQL语法错误以及解决方法

sql常见语法_常见SQL语法错误以及解决方法

时间:2020-04-24 22:59:32

相关推荐

sql常见语法_常见SQL语法错误以及解决方法

sql常见语法

SQL Server Management Studio, errors can be tracked down easily, using the built in错误列表”窗格,可以轻松跟踪Error Listpane. This pane can be activated in theSQL Server Management Studio错误。 可以在“Viewmenu, or by using shortcuts视图”菜单中或使用快捷键Ctrl+\andCtrl + \和Ctrl+ECtrl + E激活此窗格。

The Error List pane displays syntax and semantic errors found in the query editor. To navigate directly to the SQL syntax error in the script editor, double-click the corresponding error displayed in the Error List

“错误列表”窗格显示在查询编辑器中发现的语法和语义错误。 要在脚本编辑器中直接导航到SQL语法错误,请双击“错误列表”中显示的相应错误。

SQL关键字错误 (SQL Keyword errors)

SQL keyword errors occur when one of the words that the SQL query language reserves for its commands and clauses is misspelled. For example, writing “UPDTE” instead of “UPDATE” will produce this type of error

当SQL查询语言为其命令和子句保留的单词之一拼写错误时,就会发生SQL关键字错误。 例如,写“ UPDTE”而不是“ UPDATE”将产生这种类型的错误。

In this example, the keyword “TABLE” is misspelled:

在此示例中,关键字“ TABLE”的拼写错误:

As shown in the image above, not only the word “TBLE” is highlighted, but also the words around it. The image below shows that this simple mistake causes many highlighted words

如上图所示,不仅单词“ TBLE”突出显示,而且周围的单词也突出显示。 下图显示此简单错误导致许多突出显示的单词

In fact, there are total of 49 errors reported just because one keyword is misspelled

实际上,仅由于一个关键字拼写错误,总共报告了49个错误

If the user wants to resolve all these reported errors, without finding the original one, what started as a simple typo, becomes a much bigger problem

如果用户想解决所有这些报告的错误,而又找不到原始错误,那么从一个简单的错字开始就变成了一个更大的问题。

It’s also possible that all SQL keywords are spelled correctly, but their arrangement is not in the correct order. For example, the statement “FROM Table_1 SELECT *” will report an SQL syntax error

所有SQL关键字的拼写也可能正确,但是它们的排列顺序不正确。 例如,语句“ FROM Table_1 SELECT *”将报告SQL语法错误

命令安排 (Arrangement of commands)

The wrong arrangement of keywords will certainly cause an error, but wrongly arranged commands may also be an issue

关键字排列错误肯定会导致错误,但是命令排列错误也可能是一个问题

If the user, for example, is trying to create a new schema into an existing database, but first wants to check if there is already a schema with the same name, he would write the following command

例如,如果用户试图在现有数据库中创建新模式,但是首先要检查是否已经存在一个具有相同名称的模式,则他将编写以下命令

However, even though each command is properly written, and is able to run separately without errors, in this form it results in an error

但是,即使正确地编写了每个命令,并且能够独立运行且没有错误,以这种形式也会导致错误

As the error message states, CREATE SCHEMA command has to be the first command that is given. The correct way of running this commands together looks like this

如错误消息所述,CREATE SCHEMA命令必须是给出的第一个命令。 一起运行此命令的正确方法如下所示

使用引号 (Using quotation marks)

Another common error that occurs when writing SQL project is to use double quotation marks instead of single ones. Single quotation marks are used to delimit strings. For example, double quotation marks are used here instead of single ones, which cause an error

编写SQL项目时发生的另一个常见错误是使用双引号而不是单引号。 单引号用于分隔字符串。 例如,此处使用双引号而不是单引号,这会导致错误

Replacing quotation marks with the proper ones, resolves the error

用正确的引号替换引号可以解决错误

There are situations where double quotation marks need to be used, for writing some general quotes, for example

在某些情况下,需要使用双引号来编写一些通用引号,例如

As shown in the previous example, this will cause an error. But, this doesn’t mean that double quotes can’t be used, they just have to be inside the single quotes. However, adding single quotes in this example won’t solve the problem, but it will cause another one

如上例所示,这将导致错误。 但是,这并不意味着不能使用双引号,而只需要在单引号内即可。 但是,在此示例中添加单引号不会解决问题,但会导致另一个问题

Since there is an apostrophe inside this quote, it is mistakenly used as the end of a string. Everything beyond is considered to be an error

由于此引号内有撇号,因此将其错误地用作字符串的结尾。 超出范围的所有内容均视为错误

To be able to use an apostrophe inside a string, it has to be “escaped”, so that it is not considered as a string delimiter. To “escape” an apostrophe, another apostrophe has to be used next to it, as it is shown below

为了能够在字符串中使用撇号,必须对其进行“转义”,以便不将其视为字符串定界符。 要“转义”一个撇号,必须在其旁边使用另一个撇号,如下所示

查找SQL语法错误 (Finding SQL syntax errors)

Finding SQL syntax errors can be complicated, but there are some tips on how to make it a bit easier. Using the aforementioned Error List helps in a great way. It allows the user to check for errors while still writing the project, and avoid later searching through thousands lines of code

查找SQL语法错误可能很复杂,但是有一些技巧使它变得更简单。 使用上述错误列表有很大帮助。 它允许用户在仍然编写项目的同时检查错误,并避免以后搜索数千行代码

Another way to help, is to properly format the code

另一种帮助方法是正确格式化代码

This can improve code readability, thus making the search for errors easier

这样可以提高代码的可读性,从而使错误查找更加容易

翻译自: /sql-syntax-errors/

sql常见语法

如果觉得《sql常见语法_常见SQL语法错误以及解决方法》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。