1
-- --------------------------------------------------------------------------------
-- 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,但是当我尝试从 table 中选择时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")); 
    }
4

2 回答 2

2

听起来您正试图choices_summed_storage从不同的连接中进行选择,但是

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

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

于 2012-06-20T03:19:35.537 回答
0

我已经弄清楚我们出了什么问题。我已将输入定义storage_calc_usage为,BIGINT但在我用来传递值的 java 代码setInt中,并且在 sql 中我有IF storage_calc_usage > 0 THEN这个条件失败,因此没有创建临时表。

于 2012-06-25T02:01:18.637 回答