具有DELETE,INSERT和SELECT的MySQL存储过程

时间:2018-09-27 04:11:45

标签: mysql

 Please give me an working example of MySQL Stored Procedure with DELETE, INSERT, and SELECT, which should be something like this

 begin  delete from TEMP_DATA   where SEQ_ID = 614  ;  INSERT INTO TEMP_DATA   (COL1,     COL2,     COL3,     COL4,     COL5,     COL6, COL7,    SEQ_ID,   LASTUPDATE,     USER_CODE)   SELECT item_code, item_name,rate, um, item_catg_code, (SELECT item_catg_name from item_catg_mast C where C.item_catg_code =  I.item_catg_code) item_catg_name,item_desc, 614 SEQ_ID, SYSDATE() LASTUPDATE,  'AKASH' USER_CODE FROM item_mast I;  end;

我已经写了这个查询,但是没有得到预期的输出。

1 个答案:

答案 0 :(得分:1)

尝试如下操作:

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')

尝试使用select语句插入表:

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