MySQL 错误代码 1046:未选择数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24190575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:38:23  来源:igfitidea点击:

Error Code 1046: No database Selected

mysqlstored-procedures

提问by rocky

I wrote a stored procedure (sp_archivev3) on MySQl Workbench which is as follows. Basically, Inserting values from one database to another.

我在 MySQl Workbench 上写了一个存储过程(sp_archivev3),如下所示。基本上,将值从一个数据库插入到另一个数据库。

-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`MailMe`@`%` PROCEDURE `sp_archivev3`()
BEGIN

INSERT INTO 
     send.sgev3_archive(a_bi,
                        b_vc,
                        c_int,
                        d_int,
                        e_vc,
                        f_vc,
                        g_vc,
                        h_vc,
                        i_dt,
                        j_vc,
                        k_vc,
                        l_vc,
                        m_dt,
                        n_vch,
                        o_bit)
SELECT     a_bi,
           b_vc,
           c_int,
           d_int,
           e_vc,
           f_vc,
           g_vc,
           h_vc,
           i_dt,
           j_vc,
           k_vc,
           l_vc,
           m_dt,
           n_vch,
           o_bit

FROM   send.sgev3

WHERE m_dt BETWEEN  '2014-06-09' AND CURDATE();


END

When I run call sp_archivev3();, I get an error with an error code 1046: No database selected SELECT the default DB to be used by double-clicking its name in the SCHEMAS list in the sidebar.

当我运行时call sp_archivev3();,我收到一个错误代码为 1046 的错误:没有选择数据库选择要使用的默认数据库,方法是在侧边栏的 SCHEMAS 列表中双击其名称。

Please let me know what's wrong with my stored procedure.

请让我知道我的存储过程有什么问题。

回答by spencer7593

The problem is that MySQL doesn't know which procedure named sp_archivev3is supposed to be executed; MySQL doesn't know which database to look in. (Stored programs are objects in a specific database, just like tables are objects in a specific database.)

问题是 MySQL 不知道sp_archivev3应该执行命名的哪个过程;MySQL 不知道要查找哪个数据库。(存储的程序是特定数据库中的对象,就像表是特定数据库中的对象一样。)

Either specify the current database with USEstatement:

使用USE语句指定当前数据库:

use mydatabase;
call sp_archivev3();

or qualify the procedure with the name of database:

或使用数据库名称限定过程:

call mydatabase.sp_archivev3();