简体   繁体   English

MySQL存储过程通过选择作为参数

[英]MySQL stored procedure pass select as parameter

could you please give me an advice how to CALL prcd with SELECT results? 您能否给我一个建议,如何用SELECT结果调用prcd? Or advice me pls better solution.. I am open minded to all working solution 或建议我提供更好的解决方案。.我对所有可行的解决方案持开放态度

I have a procedure to control inserting data ... 我有一个程序来控制插入数据...

CREATE PROCEDURE control_insert (
)

And I need to pass data from SELECT results to procedure ... 我需要将数据从SELECT结果传递到过程...

SELECT t.c1, t.c2
FROM table t1
LEFT JOIN other_table t2
ON t1.id = t2.id
WHERE 1=1

The point is, I need to get some data via SELECT (around 6 tables joined to the base table) and I need to do control for each row before insert.. each row should meet some conditions .. if it doesn't meet them, it should just skip it and process next one ... 关键是,我需要通过SELECT获取一些数据(大约6个表连接到基本表),并且需要在插入之前对每一行进行控制。每行应该满足一些条件..如果不满足它们,它应该跳过它并处理下一个...

The procedure should look like: 该过程应如下所示:

CREATE PROCEDURE control_insert (
IN v_c1 INT,
IN v_c2 INT
)

BEGIN 
   IF v_c1 > 1 THEN
    INSERT INTO controlled_table (id, type) VALUES (v_c1, v_c2);
   ELSE
     /* do nothing */
   END IF;
END;


CALL control_insert ( SELECT .... );

Could you help me with that? 你能帮我吗? Is there any possibility to do this via MySQL? 有可能通过MySQL执行此操作吗? I can write a PERL skript, but I want to avoid this type of solution ... I just one to do it only in MySQL way 我可以写一个PERL skript,但是我想避免这种类型的解决方案...我只是一个只能用MySQL方式做到这一点的人

Thank you 谢谢

EDIT1: I need to check if ID of the SELECT result and LABEL is already in this table for specific date ... this code above is only an example to demonstrate the situation EDIT1:我需要检查SELECT结果的ID和LABEL是否已在特定日期的表中...上面的代码仅是演示这种情况的示例


SOLUTION

I've found the solution ... so for the other visitors: 我已经找到了解决方案...因此对于其他访客:

calling procedure: 调用过程:

CALL controlInsert();

procedure body: 程序主体:

CREATE PROCEDURE controlInsert()
BEGIN

  DECLARE done INT DEFAULT FALSE;
  DECLARE v_id INT;
  DECLARE v_id_dupl INT;
  DECLARE v_label INT;
  DECLARE v_date DATE;
  DECLARE v_type VARCHAR(100);
  DECLARE v_category VARCHAR(255);
  DECLARE v_user VARCHAR(255);
  DECLARE v_country VARCHAR(255);   

  DECLARE c1 CURSOR FOR SELECT id, label, date, type, category, user, country FROM t1 LEFT JOIN ... /* whole select with 6 joins ended by ; */

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;       

    ## open cursor
    OPEN c1;

        ## loop through the cursor
        read_loop: LOOP

          ## fetch cursor into variables
            FETCH c1 INTO v_id , v_label, v_date, v_type, v_category, v_user, v_country;

            ## check if there is any record
            IF done THEN
              LEAVE read_loop;
            END IF;  


            ## get count of existing records 
            SELECT count(*) INTO v_id_dupl 
            FROM  
            WHERE 1=1
            AND id = v_id 
            AND label= v_label
            AND date = v_date;


            ## if v_id_dupl = 0 => no rows found  (ok to load)
            IF (v_id_dupl = 0) THEN 

              INSERT INTO target_table (id, label, date, type, category, user, country) 
                          VALUES (v_id , v_label, v_date, v_type, v_category, v_user, v_country);

          END IF; 

        END LOOP;

    CLOSE c1;       

END

If that is all your stored procedure is doing, then you don't actually need it. 如果这就是您的存储过程所做的全部,那么您实际上就不需要它了。 You can do the whole thing in a single statement: 您可以在一个语句中完成全部操作:

INSERT INTO controlled_table (id, type)
SELECT t.c1, t.c2
FROM table t1
LEFT JOIN other_table t2 ON t1.id = t2.id
WHERE something = somethingElse
AND t.c1 > 1

Essentially, I've just combined your original query with the INSERT statement in your procedure. 本质上,我只是将原始查询与过程中的INSERT语句组合在一起。

If your procedure is more complex and needs to do multiple operations on each row, then you should look into using a cursor. 如果您的过程更复杂,并且需要在每一行上执行多个操作,则应使用游标进行调查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM