我有一个mysql查询作为存储过程的一部分写入
-- {parameter}
SELECT id INTO emailId FROM email_list WHERE email = {email};
如果此查询返回空结果集,则emailId的值是多少? undefined,null,0
我想检查emailId是否包含值并运行一系列插入。
我可以使用COUNT或IFNULL,但这意味着要声明另一个要检查的变量。如果可能,我想避免它。
*****************************编辑***************** ***********
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `addCustomer`(
IN email1 VARCHAR(60),
IN status1 VARCHAR(15),
IN bill_pin_id SMALLINT UNSIGNED,
IN bill_addr VARCHAR(175),
IN bill_name VARCHAR(70),
IN tel VARCHAR(15)
)
BEGIN
DECLARE emailId INT UNSIGNED DEFAULT 0;
DECLARE billAddrId INT UNSIGNED DEFAULT 0;
DECLARE custId INT UNSIGNED DEFAULT 0;
DECLARE orderId INT UNSIGNED DEFAULT 0;
DECLARE sqlError TINYINT DEFAULT FALSE;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET sqlError = true;
-- get id of email if it exists
SELECT id INTO emailId FROM email_list WHERE email = email1;
-- if email exists, check if a customer is listed
IF (emailId > 0) THEN
SELECT id INTO custId FROM customer WHERE email_list_id = emailId;
IF (custId > 0) THEN
-- check for duplicate address in the address table
SELECT address_id INTO billAddrId FROM customer_addr
INNER JOIN address ON address_id = address.id
WHERE customer_id = custId AND address = bill_addr;
END IF;
END IF;
START TRANSACTION;
-- if emails isnt listed - no customer or address should exist
IF (emailId = 0) THEN
INSERT INTO email_list (email, status) VALUES (email1, status1);
SELECT LAST_INSERT_ID() INTO emailId;
SELECT emailId;
INSERT INTO customer (email_list_id, full_name, phone) VALUE (emailId, bill_name, tel);
SELECT LAST_INSERT_ID() INTO custId;
SELECT custId;
INSERT INTO address (pincode_id, address) VALUES (bill_pin_id, bill_addr);
SELECT LAST_INSERT_ID() INTO billAddrId;
SELECT billAddrId;
INSERT INTO customer_addr (address_id, customer_id) VALUES (billAddrId, custId);
SELECT LAST_INSERT_ID();
INSERT INTO orders (customer_id, order_status) VALUES (custId, 'pending');
SELECT LAST_INSERT_ID() INTO orderId;
END IF;
IF sql_error = FALSE THEN
COMMIT;
SELECT 'SUCCESS';
ELSE
ROLLBACK;
SELECT 'FAILED';
END IF;
END$$
DELIMITER ;
我删除了代码中不必要的行以保持简短。
以下是select语句返回的值。所有表都是空的。但是以下值表示已插入行。但它仍然是空的。 电子邮件ID 43
CUSTID 12
billAddrId 13
CustomerAddr 13
的orderId 8
答案 0 :(得分:1)
您可以通过选择值来测试它:
for (int i = 2 ; i < 27 ; i++) {
if (27 % i == 0) {
System.out.println("Found the two numbers: " + i + " and " + 27 / i);
break;
}
}
这将返回drop procedure if exists test_proc;
create procedure test_proc()
begin
DECLARE emailId INT UNSIGNED;
SET emailId = 0;
SELECT 123 INTO emailId FROM dual WHERE 1 = 2;
SELECT emailId;
end;
call test_proc();
,这意味着 - 如果没有找到行,则不会更改该值。
答案 1 :(得分:0)
为我工作
MariaDB [sandbox]> drop procedure if exists p;
Query OK, 0 rows affected (0.06 sec)
MariaDB [sandbox]> delimiter $$
MariaDB [sandbox]> create procedure p(inid int)
-> begin
-> declare vid int;
-> set vid = 0;
-> select id into vid from users where id = inid;
-> if vid = 1 then
-> select 'found';
-> else
-> select 'notfound';
-> end if;
-> end $$
Query OK, 0 rows affected (0.05 sec)
MariaDB [sandbox]> delimiter ;
MariaDB [sandbox]>
MariaDB [sandbox]> call p(1);
+-------+
| found |
+-------+
| found |
+-------+
1 row in set (0.02 sec)
Query OK, 0 rows affected (0.02 sec)
MariaDB [sandbox]>
MariaDB [sandbox]> call p(1000);
+----------+
| notfound |
+----------+
| notfound |
+----------+
1 row in set (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.01 sec)
MariaDB [sandbox]> show warnings;
+---------+------+-----------------------------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------------------------+
| Warning | 1329 | No data - zero rows fetched, selected, or processed |
+---------+------+-----------------------------------------------------+
1 row in set (0.00 sec)