首页 > 数据库 > MySQL > 正文

MySQL基础 创建bc网站sql执行错误时的控制方式

2024-07-24 12:32:12
字体:
来源:转载
供稿:网友
        行语句的正常执行
 
       搭建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]}

(编辑:武林网)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表