使用mysql存储过程创建多个视图

时间:2019-03-15 13:57:21

标签: mysql

我想建立约900个视图(1个表1个视图,并基于2个条件),并且我在mysql环境中的一个表中拥有所有表名。我已经创建了一个存储过程。为了测试目的,我使用了限制1。

当我调用sp时,出现错误。 实际上,我重新编写了一个mssql sp,在这里我没有错过任何内容吗?请帮助

  

CALL ca_uimsp_viewcreation_ontime()错误代码:1064。您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第0.000秒的第1行使用“ NULL”附近

    USE `ca_uim`;
DROP procedure IF EXISTS `sp_viewcreation_ontime`;

DELIMITER $$
USE `ca_uim`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_viewcreation_ontime`()
BEGIN
DECLARE qos varchar(255); 
DECLARE pos int; 
DECLARE r_table varchar(255); 
DECLARE view varchar(255); 
DECLARE cview varchar(2048);
DECLARE done int default 0;

DECLARE qos_cursor CURSOR FOR SELECT DISTINCT qos,r_table FROM S_QOS_DATA ORDER BY 2 limit 1;
DECLARE continue handler for not found set done = 1;

OPEN qos_cursor;

-- Perform the first fetch.
FETCH qos_cursor INTO qos, r_table;

-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE not done
DO

                -- Check QOS name for '-' character & replace with '_' if exist
                SET pos = LOCATE('-',qos, 1);
                IF pos != 0
                THEN
                                SET qos = INSERT(qos, pos, 1, '_');
                END IF;

                -- Check QOS name for '/' character & replace with '_' if exist
                SET pos = LOCATE('/',qos, 1);
                IF pos != 0
                THEN
                                SET qos = INSERT(qos, pos, 1, '_');
                END IF;

                -- Check QOS name for '(' character & replace with '_' if exist
                SET pos = LOCATE('(',qos, 1);
                IF pos != 0
                THEN
                                SET qos = INSERT(qos, pos, 1, '_');
                END IF;

                -- Check QOS name for ')' character & replace with '_' if exist
                SET pos = LOCATE(')',qos, 1);
                IF pos != 0
                THEN
                                SET qos = INSERT(qos, pos, 1, '_');
                END IF;

                -- Create view
                SET view = CONCAT('V_',qos);
                SET cview = CONCAT('CREATE VIEW ',view,' AS ',
                                'SELECT Q.source,Q.target,Q.origin,Q.robot,Q.probe,D.sampletime,D.samplevalue,D.samplestdev,D.samplerate,D.tz_offset ',
                                'FROM S_QOS_DATA Q JOIN ',r_table,' D ON Q.table_id=D.table_id');

                BEGIN
                                -- Suppress Error message for Views that don't exist
                                DECLARE CONTINUE HANDLER FOR 1051
                                set @stmt_str =  CONCAT('DROP VIEW ',view);
                                prepare stmt from @stmt_str;
                                execute stmt;
                                deallocate prepare stmt;
                END;

                BEGIN
                                -- Create the View, Catch tables that don't have samplestdev & samplerate fields
                                DECLARE CONTINUE HANDLER FOR 1054
                                set @stmt_str =  cview;
                                prepare stmt from @stmt_str;
                                execute stmt;
                                deallocate prepare stmt;
                                /* PRINT CONCAT('Created View: ' , view) */
                END;
                BEGIN
                                DECLARE CONTINUE HANDLER FOR 1054
                                SET cview = CONCAT('CREATE VIEW ',view,' AS ',
                                'SELECT Q.source,Q.target,Q.origin,Q.robot,Q.probe,D.sampletime,D.samplevalue,D.tz_offset ',
                                'FROM S_QOS_DATA Q JOIN ',r_table,' D ON Q.table_id=D.table_id');
                                set @stmt_str =  cview;
                                prepare stmt from @stmt_str;
                                execute stmt;
                                deallocate prepare stmt;
                                /* PRINT CONCAT('Created View: ' , view) */
                END;

                -- PRINT 'qos: ' + @qos + ' ' +  @r_table+' '+@view
                -- PRINT @cview
                -- This is executed as long as the previous fetch succeeds.
                FETCH qos_cursor INTO qos, r_table;

END WHILE;

CLOSE qos_cursor;
END$$

DELIMITER ;

谢谢

0 个答案:

没有答案