MySQL / Mariadb - 从存储过程中选择*

时间:2017-12-29 22:45:38

标签: mysql stored-procedures prepared-statement mariadb

我正在尝试使用UF Dashbuilder为我的SQL查询添加语法,但我不知道如何纠正它。

有效的通话类似于:

call database.report ('1234', 'txt');

Dashbuilder将其变为不起作用:

 SELECT * FROM (call database.report ('1234', 'txt');) AS `dbSQL` LIMIT 1

我还可以使用存储过程末尾的代码将结果存储在表格中,然后在Dashbuilder中使用SELECT * FROM TABLE,但我不知道如何将结果存储在表格中(动态列数)。

您能否告诉我如何使用SELECT *添加存储过程或如何将此代码的结果存储在表格中?

  SET @sql = NULL;
   SET SESSION GROUP_CONCAT_MAX_LEN = 1000000;  -- default is 1024
   SELECT
      GROUP_CONCAT(DISTINCT
         CONCAT(
            'MAX(IF(question = ''', REPLACE(question,"'", "\\'"), ''', answer, NULL)) AS ''', REPLACE(question,"'", "\\'"), ''''
         )
      ) INTO @sql
      FROM tmp2;

      SET @sql = CONCAT('SELECT id, datestamp, ', @sql, ' FROM tmp2 GROUP BY id');

      -- SELECT @sql;   

      PREPARE stmt FROM @sql;
      EXECUTE stmt;
      DEALLOCATE PREPARE stmt;  

更新1。   对答案的评论1: 原始@SQL的结果:

`SET @sql = CONCAT('SELECT id, datestamp, ', @sql, ' FROM tmp2 GROUP BY id');`


SELECT id, datestamp, MAX(IF(question = '1. Our records show that you got care from the provider named below in the last 6 months. {Provider}.  Is that right?', answer, NULL)) 
   AS '1. Our records show that you got care from the provider named below in the last 6 months. {Provider}.  Is that right?',
   MAX(IF(question = '2. Is this the provider you usually see if you need a check-up, want advice about a health problem, or get sick or hurt?', answer, NULL)) 
   AS '2. Is this the provider you usually see if you need a check-up, want advice about a health problem, or get sick or hurt?',
   MAX(IF(question = 'Area', answer, NULL)) AS 'Area',MAX(IF(question = 'Encounter', answer, NULL)) AS 'Encounter' FROM tmp2 GROUP BY id

@SQL的结果:

错误:错误代码:1166。列名不正确' 1。我们的记录显示您在过去6个月内得到了以下提供者的护理。 {提供商}'

我猜有一个它不喜欢的字符,例如单引号?

SET @tableName = 'myreport';
SET @sql = CONCAT('CREATE TABLE ', @tableName, ' AS SELECT id, datestamp, ', @sql, ' FROM tmp2 GROUP BY id');


CREATE TABLE myreport AS SELECT id, datestamp, MAX(IF(question = '1. Our records show that you got care from the provider named below in the last 6 months. {Provider}.  Is that right?', answer, NULL)) 
   AS '1. Our records show that you got care from the provider named below in the last 6 months. {Provider}.  Is that right?',
   MAX(IF(question = '2. Is this the provider you usually see if you need a check-up, want advice about a health problem, or get sick or hurt?', answer, NULL)) 
   AS '2. Is this the provider you usually see if you need a check-up, want advice about a health problem, or get sick or hurt?',
   MAX(IF(question = 'Area', answer, NULL)) AS 'Area',MAX(IF(question = 'Encounter', answer, NULL)) AS 'Encounter' FROM tmp2 GROUP BY id

更新2: 这个工作!!!谢谢! 我必须减少列名的长度,如下所示。然后我可以每天运行两次存储过程并从Dashbuilder中的这个表中选择*。

CREATE TABLE myreport AS SELECT id, datestamp, MAX(IF(question = '1. Our records show that you got care from the provider named below in the last 6 months. {Provider}.  Is that right?', answer, NULL)) 
   AS '1.',
   MAX(IF(question = '2. Is this the provider you usually see if you need a check-up, want advice about a health problem, or get sick or hurt?', answer, NULL)) 
   AS '2.',
   MAX(IF(question = 'Area', answer, NULL)) AS 'Area',MAX(IF(question = 'Encounter', answer, NULL)) AS 'Encounter' FROM tmp2 GROUP BY id

更新3:这有效!谢谢!

   SET @t = CONCAT('DROP TABLE IF EXISTS ', survey_report );
   PREPARE stmt FROM @t;
   EXECUTE stmt;
   DEALLOCATE PREPARE stmt;

   SET @sql = NULL;
   SET SESSION GROUP_CONCAT_MAX_LEN = 1000000;  -- default is 1024
   SELECT
      GROUP_CONCAT(DISTINCT
         CONCAT(  
            'MAX(IF(question = ''', REPLACE(question,"'", "\\'"), ''', answer, NULL)) AS ''', REPLACE(udf_clean_column_name(15, udf_remove_tags(question)),"'", "\\'"), ''''
         )
      ) INTO @sql
      FROM tmp2;

      SET @sql = CONCAT('CREATE TABLE ', survey_report, ' AS SELECT id, datestamp, ipaddr, ', @sql, ' FROM tmp2 GROUP BY id');

      -- SELECT @sql;

      PREPARE stmt FROM @sql;
      EXECUTE stmt;
      DEALLOCATE PREPARE stmt;   

   DROP TEMPORARY TABLE IF EXISTS `survey_lookup`;
   DROP TEMPORARY TABLE IF EXISTS `tmp`;
   DROP TEMPORARY TABLE IF EXISTS `tmp2`;

Mysql用于从列名中删除空格等的函数。

CREATE FUNCTION `udf_clean_column_name`(col_name_len INT, str varchar(200)) RETURNS varchar(200)
    BEGIN
       SET str = SUBSTRING(str,1,col_name_len);
       SET str = TRIM(str);
       SET str = Replace(str,' ','_');
       SET str = Replace(str,'-','');
       SET str = Replace(str,'?','');
       SET str = Replace(str,',','');
       SET str = Replace(str,'.','');
       RETURN str;
    END;

用于删除标签的Mysql函数(我不记得我在哪里获得此功能)。

CREATE FUNCTION `udf_remove_tags`(Dirty varchar(4000))
RETURNS varchar(4000)
    DETERMINISTIC
    BEGIN
      DECLARE iStart, iEnd, iLength int;
        WHILE Locate( '<', Dirty ) > 0 And Locate( '>', Dirty, Locate( '<', Dirty )) > 0 DO
          BEGIN
            SET iStart = Locate( '<', Dirty ), iEnd = Locate( '>', Dirty, Locate('<', Dirty ));
            SET iLength = ( iEnd - iStart) + 1;
            IF iLength > 0 THEN
              BEGIN
                SET Dirty = Insert( Dirty, iStart, iLength, '');
                set Dirty = Replace(Dirty,'&nbsp;',''); #No space between & and nbsp;
                set Dirty = Replace(Dirty,'\r','');
                set Dirty = Replace(Dirty,'\n','');
              END;
            END IF;
          END;
        END WHILE;
        RETURN Dirty;
    END;

1 个答案:

答案 0 :(得分:1)

我在UF DashBuilder文档中没有看到任何存储过程的提及,所以看起来他们没有办法解决这个问题。

您可以从SELECT查询创建表格。如果省略列规范,则会从查询的选择列表中自动导出它们。

SET @sql = CONCAT('CREATE TABLE ', tableName, ' AS
    SELECT id, datestamp, ', @sql, ' FROM tmp2 GROUP BY id');
PREPARE stmt FROM @sql
EXECUTE stmt;
DEALLOCATE PREPARE stmt;