MySQL谜题:如何将结果组合成Loop?

时间:2018-05-15 06:21:33

标签: mysql stored-procedures

到目前为止,我已尝试使用
CREATE VIEW <view_table_name>
CREATE TABLE <table_name>CREATE TEMPORARY <temporary_table_name> (step 1) create a table 这种方法花了很多时间,因为我必须(step 2) insert a data(step 3) select the created table。然后是(step 4) drop the table。最后,BEGIN -- Main loop variables DECLARE col_Name varchar(255); DECLARE col_Description varchar(255); -- Main Loop Block2: BEGIN DECLARE done BOOLEAN DEFAULT FALSE; DECLARE statement CURSOR FOR SELECT `name`, `description` FROM `rules`; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; OPEN statement; REPEAT MainLoop: LOOP FETCH statement INTO col_Name, col_Description; IF done THEN LEAVE MainLoop; END IF; SELECT col_Name AS `Name`, col_Description AS `Description`; END LOOP MainLoop; UNTIL done END REPEAT; CLOSE statement; END Block2; -- End of Main Loop END

这是我的程序:(我的程序看起来没有任何意义,我的意思是为什么我这样做。你的困惑的答案是,这只是我的一部分大代码,我这样做,这看起来很简单。)

string timestamp = DateTime.Now.ToString( "yyyy-MM-dd_HH-mm-ss", DateTimeFormatInfo.InvariantInfo );
....
.WriteTo.File( $@"logs\Info_{timestamp}.txt", retainedFileCountLimit: 90, rollingInterval: RollingInterval.Day, restrictedToMinimumLevel: LogEventLevel.Information, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}]<{ThreadName}({ThreadId})> {Message:lj}{NewLine}{Exception}" )

结果是:
enter image description here

问题:
结果是分开的。如何将此结果合并到一个表中?

2 个答案:

答案 0 :(得分:2)

在开头声明一个临时表,将数据填充到临时表中,然后在循环结束后访问它

BEGIN
        -- Main loop variables
        DECLARE col_Name varchar(255);
        DECLARE col_Description varchar(255);
        create temporary table yourtable (name varchar(50),description varchar(50));

        -- Main Loop
        Block2: BEGIN
            DECLARE done BOOLEAN DEFAULT FALSE;
            DECLARE statement CURSOR FOR SELECT `name`, `description` FROM `rules`;
            DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

            OPEN statement;
                REPEAT
                    MainLoop: LOOP
                        FETCH statement INTO col_Name, col_Description;

                        IF done THEN
                            LEAVE MainLoop;
                        END IF;
                        insert into yourtable(name,description)
 values (col_Name, col_Description)


                    END LOOP MainLoop;

                UNTIL done END REPEAT;
            CLOSE statement;
        select * from yourtable
        END Block2;
        -- End of Main Loop
    END

答案 1 :(得分:0)

我认为事情会变得更容易。

您不必使用游标,只需在过程中创建一个空表,然后输入查询的结果即可。

如下所示:

CREATE DEFINER=`root`@`localhost` PROCEDURE `temp`(val int)

BEGIN

DECLARE yourval datatype()
-- declare as many as you need.---

set @beginning = 0
set @ending = val
-- how many time do you need to repeat your code?

---CREATE AN EMPTY TABLE HERE---

while @beginning < @ending do
insert table TableCreatedAbove 

---YOUR QUERY---
---YOU MAY NEED TO DO SOMETHING WITH YOUR DECLARED VARIABLE---

set @beginning = @beginning + 1;

end while;
commit;
END

当您需要使用合并表时,只需

select * from TableCreatedAbove 

也许您已经在其他地方解决了问题,但这是我的答案。