我阅读手册,我可以创建简单的存储过程。 它的工作很好。但是现在我需要创建复杂的程序,包括DELETE,SELECT和INSERT
首先:
delete from 111test;
下一步:
SELECT u.user_id, u.user_nickname, u.country_id, u.city_id, u.avatar_id,
(SELECT name FROM countries WHERE id = u.country_id) country_name,
(SELECT name FROM cities WHERE id = u.city_id) city_name
FROM users_onsite uo
LEFT JOIN users u ON u.user_id = uo.user_id
WHERE u.avatar_id > 0;
接下来:
INSERT INTO 111test (type,user_id,some_datas1,some_datas2,some_datas3)
values (53,$user_id,'$user_nickname',$country_name','$city_name')
所以,我的程序是:
BEGIN
DECLARE var_user_id INT(50);
DECLARE var_some_datas1 varchar(255);
DECLARE var_some_datas2 varchar(255);
DECLARE var_some_datas3 varchar(255);
delete from 111test;
SELECT u.user_id, u.user_nickname, u.country_id, u.city_id, u.avatar_id,
(SELECT name FROM countries WHERE id = u.country_id) country_name,
(SELECT name FROM cities WHERE id = u.city_id) city_name
FROM users_onsite uo
LEFT JOIN users u ON u.user_id = uo.user_id
WHERE u.avatar_id > 0;
INSERT INTO 111test (type,user_id,some_datas1,some_datas2,some_datas3)
VALUES (53,var_user_id,var_some_datas1,var_some_datas2,var_some_datas3);
END
结果:您的SQL查询已成功执行1172行受过程
中最后一个语句的影响但是在表111中插入了ZERO行。
请有人帮助我。
答案 0 :(得分:1)
那么,这里有什么障碍。您可以创建包含DML操作的过程。请参阅下面的示例(尽管在SQL Server中创建)
create procedure sp_crud_test
as
begin
delete from sample1;
select * from sample3;
insert into sample1 select * from sample3;
end
exec sp_crud_test
所以在你的情况下,它会像
delimiter //
CREATE PROCEDURE sp_crud_test (IN
user_id int, IN user_nickname varchar(10),
IN country_name varchar(10),IN city_name varchar(10))
BEGIN
delete from 111test;
SELECT u.user_id, u.user_nickname, u.country_id, u.city_id, u.avatar_id,
(SELECT name FROM countries WHERE id = u.country_id) country_name,
(SELECT name FROM cities WHERE id = u.city_id) city_name
FROM users_onsite uo
LEFT JOIN users u ON u.user_id = uo.user_id
WHERE u.avatar_id > 0;
INSERT INTO 111test (type,user_id,some_datas1,some_datas2,some_datas3)
values (53,user_id,user_nickname,country_name,city_name)
END//
比称之为
CALL sp_crud_test(10,'tk','US','london')
修改强> 现在我得到了,你试图将你的选择查询值插入到该表中;应该像下面这样做
delimiter //
CREATE PROCEDURE sp_crud_test
BEGIN
delete from 111test;
INSERT INTO 111test (type,user_id,some_datas1,some_datas2,some_datas3)
SELECT '53' as type, u.user_id, u.user_nickname,
(SELECT name FROM countries WHERE id = u.country_id) country_name,
(SELECT name FROM cities WHERE id = u.city_id) city_name
FROM users_onsite uo
LEFT JOIN users u ON u.user_id = uo.user_id
WHERE u.avatar_id > 0;
END//
然后就像
一样打电话CALL sp_crud_test