如何检查MySQL脚本中前面语句中的错误?
我正在从shell脚本运行一些MySQL脚本(用于批处理),只是使用MySQL命令行实用程序。在其中一个脚本中,我想创建一个新表,填充它,当我确信填充成功时,重命名它以替换旧表,并删除旧表,因此事件顺序如下所示:如何检查MySQL脚本中前面语句中的错误?,mysql,sql,bash,command-line,Mysql,Sql,Bash,Command Line,我正在从shell脚本运行一些MySQL脚本(用于批处理),只是使用MySQL命令行实用程序。在其中一个脚本中,我想创建一个新表,填充它,当我确信填充成功时,重命名它以替换旧表,并删除旧表,因此事件顺序如下所示: create table if not exists mystuff_tmp ( id int(10) unsigned not null auto_increment, average_value decimal(10,4) default null, day date
create table if not exists mystuff_tmp (
id int(10) unsigned not null auto_increment,
average_value decimal(10,4) default null,
day date not null,
primary key (id)
) engine=InnoDB default charset=utf8;
insert into mystuff_tmp (
average_value,
day
) select
avg(value),
date(input)
from
details
where
input between date(now()) - interval 7 day and date(now)
group by
date(input);
***if there were no errors***
rename table mystuff to mystuff_old, mystuff_tmp to mystuff;
drop table mystuff_old;
在Sybase(及其子项,如MS SQL)中,存在@错误,
但是我在MySQL中发现的唯一类似的系统变量是error\u count,它不会因会话错误而更新…我所做的只是将shell脚本分解成几个部分。例如:
mysql_cmd="mysql -u$mysql_user -p$mysql_pw $db"
$mysql_cmd <<eot
create table if not exists mystuff_tmp (
id int(10) unsigned not null auto_increment,
average_value decimal(10,4) default null,
day date not null,
primary key (id)
) engine=InnoDB default charset=utf8;
insert into mystuff_tmp (
average_value,
day
) select
avg(value),
date(input)
from
details
where
input between date(now()) - interval 7 day and date(now)
group by
date(input);
eot
if [ $? != 0]
then
$mysql_cmd <<eot
rename table mystuff to mystuff_old, mystuff_tmp to mystuff;
drop table mystuff_old;
eot
fi
mysql\u cmd=“mysql-u$mysql\u user-p$mysql\u pw$db”
$mysql_cmdmysql必须处理各种错误和警告。在这些错误处理程序中,可以为变量指定值。在重命名操作之前,您可以检查变量的值。我认为
SELECT @@session.warning_count;
或
会给你想要的谢谢。这是我过去一直在做的事情,但我有一种情况,在这种情况下,最好用一个脚本来处理每一个案例。非常感谢。
SELECT @@session.error_count;