Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Code 1046: No database Selected

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

-- --------------------------------------------------------------------------------
-- 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.

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

like image 258
rocky Avatar asked Jun 12 '14 17:06

rocky


People also ask

How do I fix error code 1046 in MySQL?

Now, we can choose any database. Suppose I am using the database 'business', therefore we can choose with the help of 'use' command. After using database 'business', we can create the above table and we will not get any error.

How do I fix error no database selected?

You need to replace [database_name] with the name of a database that exists in your MySQL server. You can also list the names of all databases available on your server with the SHOW DATABASES command. The error should be resolved once mysql responds with Database changed as shown above.

How do I fix #1046 No database selected?

Error 1046 occurs when we miss to connect our table with a database. In this case, we don't have any database and that's why at first we will create a new database and then will instruct to use that database for the created table.


1 Answers

The problem is that MySQL doesn't know which procedure named sp_archivev3 is 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.)

Either specify the current database with USE statement:

use mydatabase;
call sp_archivev3();

or qualify the procedure with the name of database:

call mydatabase.sp_archivev3();
like image 139
spencer7593 Avatar answered Sep 25 '22 23:09

spencer7593