MySQL错误1146存储过程

时间:2016-04-27 21:55:10

标签: mysql innodb

我正在尝试使用以下存储过程为表设置值。

Create Definer = Current_User Procedure sp_set_drivers_cost (In driver_id Int)
Not Deterministic
Begin
-- Variable that will holds the driver's total cost. 
Declare driver_cost Numeric(65,2) Default 0;
-- Declaring loop_counter and a varialbe that will hold the sum of the cost.
Declare loop_counter, temp_table_sum Int Default 1;
-- Variable that will hold the driver's total cost.
Declare local_cost Numeric(65,2) Default 0;

Declare temp1 int;

-- Dropping table if exists.
Drop Table If Exists MyDB.temp_policy_id;

-- Creating temp table that will hold the policy ids that the given driver is covered for.
Create Table MyDB.temp_policy_id (
    id int not null auto_increment,
    policy_id int not null default 0,
    Primary Key (id)
 ); 

 -- Inserting the policy ids for the given driver id;
 Insert Into MyDB.temp_policy_id(policy_id)
 Select fk_policy_id
 From link_drivers_policies
 Where fk_driver_id = driver_id;

-- Counting the rows of the temp table.
Set temp_table_sum = (Select Count(*) From temp_policy_id);

-- Looping through the rows of the table in order to get the total cost for each driver.
While loop_counter <= temp_table_sum Do
    Set temp1 = (Select policy_id From temp_policy_id Where id = loop_counter);
    -- Getting the total cost, for the given driver, based on the policies they are covered by.
    Set local_cost = local_cost + (Select cost From policies Where id = temp1);

    -- Incrementing the loop counter by 1.
    Set loop_counter = loop_counter + 1;
End While;

-- Updating the drivers cost.
Update MyDB.drivers
Set cost = local_cost
Where  id = driver_id;

Drop Table MyDB.temp_policy_id;
End//

当我尝试测试该过程时,通过调用它来调用“call sp_set_drivers_cost”,我收到以下错误: 调用sp_set_drivers_cost(1)错误代码:1146。表'MyDB.MyDB'不存在0.437秒。

我尝试过childinsh解决方案,比如使用返回数值的变量进行调整,省略数据库名称,然后重新启动MySQL服务(我在Windows 7上)。此外,我在过程中创建的临时表存在于数据库中。所以我的猜测是错误发生在循环中,或者在“更新”语句中。我正在使用InnoDB。谢谢。

更新 在尝试更新表“驱动程序”时,我也遇到了同样的错误。

正如@Norbert van Nobelen指出的那样,我试图实施here发布的解决方案,但无济于事。此外,我想声明我已经使用tab在我编写其余存储过程的部分中缩进我的代码,并且没有错误存在。

2 个答案:

答案 0 :(得分:0)

由于不清楚问题的来源,您最终会出现“为什么没有存储过程的调试器”的情况。

要调试编译良好然后不运行的存储过程,您可以采用以下两种方法:

  • 将记录行添加到过程
  • 将其拆开并逐步运行

选项1:添加记录行

由于日志记录行只是SELECT语句。调用该过程时,会在命令行中记录此信息。例如:

Create Definer = Current_User Procedure sp_set_drivers_cost (In driver_id Int)
Not Deterministic
Begin
-- Variable that will holds the driver's total cost. 
Declare driver_cost Numeric(65,2) Default 0;
-- Declaring loop_counter and a varialbe that will hold the sum of the cost.
Declare loop_counter, temp_table_sum Int Default 1;
-- Variable that will hold the driver's total cost.
Declare local_cost Numeric(65,2) Default 0;

Declare temp1 int;

-- Dropping table if exists.
Drop Table If Exists MyDB.temp_policy_id;

-- Creating temp table that will hold the policy ids that the given driver is covered for.
Create Table MyDB.temp_policy_id (
    id int not null auto_increment,
    policy_id int not null default 0,
    Primary Key (id)
 ); 
SELECT "Step 1 finished";
...

通过这种方式,您可以关注执行的内容和不执行的内容。

选项2:逐步分开您的程序

这类似于日志行,除了您将为每个步骤重新创建过程:

Create Definer = Current_User Procedure sp_set_drivers_cost (In driver_id Int)
Not Deterministic
Begin
-- Variable that will holds the driver's total cost. 
Declare driver_cost Numeric(65,2) Default 0;
-- Declaring loop_counter and a varialbe that will hold the sum of the cost.
Declare loop_counter, temp_table_sum Int Default 1;
-- Variable that will hold the driver's total cost.
Declare local_cost Numeric(65,2) Default 0;

Declare temp1 int;

-- Dropping table if exists.
Drop Table If Exists MyDB.temp_policy_id;

-- Creating temp table that will hold the policy ids that the given driver is covered for.
Create Table MyDB.temp_policy_id (
    id int not null auto_increment,
    policy_id int not null default 0,
    Primary Key (id)
 ); 
END;

CALL temp_policy_id;

然后是下一部分:

Create Definer = Current_User Procedure sp_set_drivers_cost (In driver_id Int)
Not Deterministic
Begin
-- Variable that will holds the driver's total cost. 
Declare driver_cost Numeric(65,2) Default 0;
-- Declaring loop_counter and a varialbe that will hold the sum of the cost.
Declare loop_counter, temp_table_sum Int Default 1;
-- Variable that will hold the driver's total cost.
Declare local_cost Numeric(65,2) Default 0;

Declare temp1 int;

-- Dropping table if exists.
Drop Table If Exists MyDB.temp_policy_id;

-- Creating temp table that will hold the policy ids that the given driver is covered for.
Create Table MyDB.temp_policy_id (
    id int not null auto_increment,
    policy_id int not null default 0,
    Primary Key (id)
 ); 

 -- Inserting the policy ids for the given driver id;
 Insert Into MyDB.temp_policy_id(policy_id)
 Select fk_policy_id
 From link_drivers_policies
 Where fk_driver_id = driver_id;
END;

CALL temp_policy_id;

等等,直到您的错误显示。大多数情况下,当问题的确切线已知时,解决方案变得更加清晰。

答案 1 :(得分:0)

在Windows上,MySQL必须忽略大小写,因为它使用表和数据库的文件和目录。 Windows无视案例。这是CREATE TABLE lcase...,而CREATE TABLE LCASE...无法在Windows上共存。

更改lower_case_table_names会遇到麻烦。