MySQL基礎:搭建bc網站sql執行錯誤時的控制方式

buzhengque發表於2019-11-07

行語句的正常執行

搭建bc網站q<277.03.4.83.6>

比如執行多條正常執行的語句,示例命令如下所示:


  select version();

  select "Hello LiuMiao" as "Greetings";

  select 20+22 as Result;


使用HereDocument方式執行如下所示:


liumiaocn:~ liumiao$ mysql -uroot -proot <<EOF

>   select version();

>   select "Hello LiuMiao" as "Greetings";

>   select 20+22 as Result;

> EOF

mysql: [Warning] Using a password on the command line interface can be insecure.

version()

8.0.11

Greetings

Hello LiuMiao

Result

42

liumiaocn:~ liumiao$ 


多行語句中間出錯時的預設動作

Oracle多行語句執行出錯時在sqlplus中會繼續執行,而在mysql控制檯中會怎樣呢?我們可以在上述語句中新增一行錯誤的語法或者命令來進行驗證:


mysql -uroot -proot <<EOF

  select version();

  errorcommand;

  select "Hello LiuMiao" as "Greetings";

  select 20+22 as Result;

EOF


執行結果如下所示


liumiaocn:~ liumiao$ mysql -uroot -proot <<EOF

>   select version();

>   errorcommand;

>   select "Hello LiuMiao" as "Greetings";

>   select 20+22 as Result;

> EOF

mysql: [Warning] Using a password on the command line interface can be insecure.

version()

8.0.11

ERROR 1064 (42000) at line 2: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'errorcommand' at line 1

liumiaocn:~ liumiao$ 


可以看到,預設情況下碰到錯誤就停下來了。


WHENEVER SQLERROR

在oracle中通過WHENEVER SQLERROR來進行控制。語法如下所示

WHENEVER SQLERROR {EXIT [SUCCESS | FAILURE | WARNING | n | variable | :BindVariable] [COMMIT | ROLLBACK] | CONTINUE [COMMIT | ROLLBACK | NONE]}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69953038/viewspace-2663030/,如需轉載,請註明出處,否則將追究法律責任。

相關文章