TEMPORARY TABLE 不存在,未按预期创建? MYSQL

Posted

技术标签:

【中文标题】TEMPORARY TABLE 不存在,未按预期创建? MYSQL【英文标题】:TEMPORARY TABLE doesn't exist, not created as expected? MYSQL 【发布时间】:2012-06-22 03:56:38 【问题描述】:
-- --------------------------------------------------------------------------------
-- Routine DDL
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `storage_choices_according_to_criteria`( 
                                        IN storage_calc_usage BIGINT,
                                        IN all_provider_considered BOOLEAN,
                                        IN provider_LIST varchar(200),
                                        IN has_requests BOOLEAN
                                        )
BEGIN

    IF storage_calc_usage > 0 THEN
        drop table if exists `storage_choices_expanded`;
        CREATE TEMPORARY TABLE `storage_choices_expanded` AS
        (select *, 
             usage_each_plan * standard_storage as price_storage, 
             concat_ws(' ',`Provider Name`,`Name`,`region_name`) as group_name
        from 
           (SELECT *, 
                ( 
                    if( 
                    (quota_band_high is not NULL) and storage_calc_usage>quota_band_high, 
                    quota_band_high, 
                    storage_calc_usage
                    ) - quota_band_low
                ) as usage_each_plan
            FROM `storage_service_price`
            where storage_calc_usage > quota_band_low
        and if(all_provider_considered, 1, find_in_set(`Provider Name`,provider_LIST))
           ) as storage_usage_each_plan
        );

        drop table if exists `request_options_for_storage`;

        if has_requests then

            CREATE TEMPORARY TABLE `request_options_for_storage` as
            SELECT 
                price_storage_requests.*,
                n,
                (n / per_unit_amount)*request_price as cost
            FROM `storage_request_criterias`
            right join price_storage_requests
            on price_storage_requests.storage_request_name = storage_request_criterias.name
            ;

        else

            CREATE TEMPORARY TABLE `request_options_for_storage` as
            select *, 
                0 as cost,
                0 as n
            from `price_storage_requests`
            where 0=1
            ;

        end if;

        drop table if exists `choices_storage_summed`;
        CREATE TEMPORARY TABLE `choices_storage_summed` AS

        select 
            summed.*,
            total_storage_cost + if(total_requests_cost is NULL,0,total_requests_cost) as 'Total Price',
            total_requests_cost,
            request_type_id_list
        from 
            (
                select *, 
                  sum(price_storage) as total_storage_cost, 
                  count(group_name) as count 
                from storage_choices_expanded
                group by group_name
            ) as summed
        left join
        (
            select 
            sum(cost) as total_requests_cost,
            GROUP_CONCAT(request_options_for_storage.id) as request_type_id_list,
            resource_type_id 
            from `request_options_for_storage`
            group by resource_type_id       
        ) as requests
        on requests.resource_type_id = summed.resource_type_id      
        where 
            (   
                count=1 
                and 
                if(quota_band_high is NULL,1,storage_calc_usage<=quota_band_high) 
            ) 
            or count>1 
        order by 'Total Price' asc
        ;

    END IF;

END

上面是我调用的 PROCEDURE,但是当我尝试从表中选择 choices_storage_summed 时,它给了我一个 Table doesn't exist 错误。

我通过 jdbc 连接以编程方式调用了 PROCEDURE,我使用的是相同的连接。 以下代码显示了我是如何调用该过程的:

private static void calcChoicesStorageSummed(Map<String, Integer> requests, boolean consider_all_provider, String provider_list, Integer storage_calc_usage, Integer daysInUse) throws SQLException 

    storage_calc_usage = storage_calc_usage * (daysInUse / DAYS_PER_MONTH);
    Boolean has_requests = insertIntoStorageRequestCriterias(requests); 

    CallableStatement cs = conn.prepareCall("call "+db+".storage_choices_according_to_criteria(?,?,?,?);"); 
    cs.setInt("storage_calc_usage", storage_calc_usage);
    cs.setBoolean("all_provider_considered", consider_all_provider);
    cs.setString("provider_LIST", provider_list); 
    cs.setBoolean("has_requests", has_requests);
    cs.executeQuery();
    cs.close();
 

这是尝试访问临时表的代码,conn 是一个全局变量,在相关操作期间保持不变。

    calcChoicesStorageSummed(requests,(provider_name_list == null),provider_name_list,storage_usage,duration);
    /**
     * debug
     */
    sql = "select * from choices_storage_summed ";
    sql += ";";

    prest = conn.prepareStatement(sql);     
    rs = prest.executeQuery();
    while (rs.next()) 
        System.out.println(rs.getString("standard_storage")); 
    

【问题讨论】:

【参考方案1】:

听起来您正试图从另一个连接中选择 choices_summed_storage,但是

TEMPORARY 表仅对当前连接可见,并在连接关闭时自动删除。

http://dev.mysql.com/doc/refman/5.6/en/create-table.html

【讨论】:

我想我对这两个操作都使用了相同的连接。请参考添加的java代码。【参考方案2】:

我已经弄清楚我们出了什么问题。 我已将输入storage_calc_usage 定义为BIGINT,但在Java 代码中我使用setInt 传递值,在sql 中我有IF storage_calc_usage &gt; 0 THEN,此条件失败,因此未创建临时表。

【讨论】:

以上是关于TEMPORARY TABLE 不存在,未按预期创建? MYSQL的主要内容,如果未能解决你的问题,请参考以下文章

postgresql----temporary table和unlogged table

材料表示例未按预期工作

查询结果未按预期返回

阅读电子邮件的 html 模板未按预期显示

将泛型与 GWT-RPC 一起使用未按预期工作

Spring @JsonIgnore 序列化未按预期工作