检查sql语句是否已执行
如何在执行另一个sql语句之前检查sql语句是否已执行 我是这样做的检查sql语句是否已执行,sql,stored-procedures,Sql,Stored Procedures,如何在执行另一个sql语句之前检查sql语句是否已执行 我是这样做的 DECLARE tempId double default 2; insert IGNORE into `system_users`( `user_id`,`username`,`password`) values (id , userName ,md5(password)) ; set tempId= last_insert_id(); IF tempId <> 0 THEN
DECLARE tempId double default 2;
insert IGNORE into `system_users`( `user_id`,`username`,`password`) values (id , userName ,md5(password)) ;
set tempId= last_insert_id();
IF tempId <> 0 THEN
insert into `user_profile`(`user_id`, `full_name`, `cellphone`, `Date_time_ added`,`added_by`) values (id, fullName ,cellPhone , CURRENT_TIMESTAMP(),addedBy ) ;
声明tempId双默认值2;
在'system\u users'('user\u id`、'username`、'password`)值(id、username、md5(password))中插入IGNORE;
设置临时id=最后一次插入id();
如果tempId为0,则
将值(id、全名、手机、当前时间戳()、addedBy)插入“用户配置文件”(“用户id”、“全名”、“手机”、“添加的日期时间”、“添加的时间”)中;
函数last_insert_id()不适用于我的案例,因为user_id不是自动递增的pk,而是用户的标识号。用于测试sql语句执行情况的函数是什么。您必须测试SQLCODE的值 SQLCA数据结构中最重要和最广泛使用的字段是SQLCODE变量。每次Microsoft®SQL Server™ 2000运行嵌入式SQL语句,它设置SQLCODE变量的值以指示上一个嵌入式SQL语句是否成功完成。值0表示上一条嵌入的SQL语句已成功。0以外的值表示警告或错误 来源 不同SQLCODE值的列表:
(我选择了MSSQL服务器,但SQLCODE是每个SQL引擎中都可以使用的标准)您必须测试SQLCODE的值 SQLCA数据结构中最重要和最广泛使用的字段是SQLCODE变量。每次Microsoft®SQL Server™ 2000运行嵌入式SQL语句,它设置SQLCODE变量的值以指示上一个嵌入式SQL语句是否成功完成。值0表示上一条嵌入的SQL语句已成功。0以外的值表示警告或错误 来源 不同SQLCODE值的列表:
(我选择了MSSQL服务器,但SQLCODE是每个SQL引擎都可以使用的标准)可能是这样的。查看插入的行是否已存在:
insert IGNORE into `system_users`( `user_id`,`username`,`password`) values (id , userName ,md5(password)) ;
IF NOT EXISTS(SELECT NULL FROM system_users WHERE system_users=id)
BEGIN
insert into `user_profile`(`user_id`, `full_name`, `cellphone`, `Date_time_ added`,`added_by`) values (id, fullName ,cellPhone , CURRENT_TIMESTAMP(),addedBy ) ;
END
也许是这样的。查看插入的行是否已存在:
insert IGNORE into `system_users`( `user_id`,`username`,`password`) values (id , userName ,md5(password)) ;
IF NOT EXISTS(SELECT NULL FROM system_users WHERE system_users=id)
BEGIN
insert into `user_profile`(`user_id`, `full_name`, `cellphone`, `Date_time_ added`,`added_by`) values (id, fullName ,cellPhone , CURRENT_TIMESTAMP(),addedBy ) ;
END
您没有指定正在使用的DMB,因此我假设MySQL(在我看来它像MySQL,如果不是,请指定)对于
INSERT
语句,使用
例如
这将返回受影响的行数
另外,请注意,对于SELECT
语句,您将使用
e、 g
尽管这将告诉您找到的总行数,但无论您是否有
LIMIT
子句。您没有指定要使用的DMB,因此我将假设MySQL(在我看来它与MySQL类似,如果不是,请指定)对于INSERT
语句,请使用
例如
这将返回受影响的行数
另外,请注意,对于SELECT
语句,您将使用
e、 g
尽管这将告诉您找到的行的总数,无论您是否有
LIMIT
子句。您可以在存储过程结束之前添加它,但它将指示是否有任何记录受到影响
IF @@ROWCOUNT <>1
BEGIN
RAISERROR ('An error occured',10,1)
RETURN -1
END
如果@@ROWCOUNT 1
开始
RAISERROR('发生错误',10,1)
返回-1
结束
为埃克斯梅普
DECLARE tempId double default 2;
insert IGNORE into `system_users`( `user_id`,`username`,`password`) values (id , userName ,md5(password)) ;
IF NOT EXISTS(SELECT NULL FROM system_users WHERE system_users=id)
BEGIN
insert into `user_profile`(`user_id`, `full_name`, `cellphone`, `Date_time_ added`,`added_by`) values (id, fullName ,cellPhone , CURRENT_TIMESTAMP(),addedBy ) ;
IF @@ROWCOUNT <>1
BEGIN
RAISERROR ('No rows were affected',10,1)
RETURN -1
END
END
声明tempId双默认值2;
在'system\u users'('user\u id`、'username`、'password`)值(id、username、md5(password))中插入IGNORE;
如果不存在(从system_users中选择NULL,其中system_users=id)
开始
将值(id、全名、手机、当前时间戳()、addedBy)插入“用户配置文件”(“用户id”、“全名”、“手机”、“添加的日期时间”、“添加的时间”)中;
如果@@ROWCOUNT 1
开始
RAISERROR('没有受影响的行',10,1)
返回-1
结束
结束
因此,如果没有修改行,RAISERROR变量将包含“没有受影响的行”。这是您正在寻找的,还是我误解了…您可以在存储过程结束之前添加它,它将指示是否有任何记录受到影响
IF @@ROWCOUNT <>1
BEGIN
RAISERROR ('An error occured',10,1)
RETURN -1
END
如果@@ROWCOUNT 1
开始
RAISERROR('发生错误',10,1)
返回-1
结束
为埃克斯梅普
DECLARE tempId double default 2;
insert IGNORE into `system_users`( `user_id`,`username`,`password`) values (id , userName ,md5(password)) ;
IF NOT EXISTS(SELECT NULL FROM system_users WHERE system_users=id)
BEGIN
insert into `user_profile`(`user_id`, `full_name`, `cellphone`, `Date_time_ added`,`added_by`) values (id, fullName ,cellPhone , CURRENT_TIMESTAMP(),addedBy ) ;
IF @@ROWCOUNT <>1
BEGIN
RAISERROR ('No rows were affected',10,1)
RETURN -1
END
END
声明tempId双默认值2;
在'system\u users'('user\u id`、'username`、'password`)值(id、username、md5(password))中插入IGNORE;
如果不存在(从system_users中选择NULL,其中system_users=id)
开始
将值(id、全名、手机、当前时间戳()、addedBy)插入“用户配置文件”(“用户id”、“全名”、“手机”、“添加的日期时间”、“添加的时间”)中;
如果@@ROWCOUNT 1
开始
RAISERROR('没有受影响的行',10,1)
返回-1
结束
结束
因此,如果没有修改行,RAISERROR变量将包含“没有受影响的行”。这是您一直在寻找的,还是我误解了…您的DBMS允许您使用事务吗?嗯,您对事务是什么意思?我指的是。您的DBMS允许您使用事务吗?嗯,您对事务是什么意思?我指的是。