juergen d's answer is of course correct; however, considering your error message, you can also add to your SQL file at the beginning line like:
USE your_database_name;
This should also do the job and let you import under the Workbench.
When you are using mysqldump
, the exported file will be saved in the current folder. It doesn't matter under what path it is. Just when importing from command line you need to be at the same folder or specify path to the file. But this isn't the case when you are using visual tool like Workbench, where you need to select the file from folder tree anyway.
BAD NEWS
You cannot use mysqldump to do this.
GOOD NEWS
You can use the mysql client. First, find out if the Stored Procedure exists. If it does, then fetch the type of Procedure it is (PROCEDURE
or FUNCTION
). Then, issue the SHOW CREATE
for the Stored Procedure. You will have to strip the top 3 and bottom 3 lines:
DBNAME=${1}
SPNAME=${2}
SPFILE=${DBNAME}_${SPNAME}.sql
SPTEMP=${DBNAME}_${SPNAME}.tmp
MYSQL_CONN="-u... -p..."
SQLSTMT="SELECT COUNT(1) FROM mysql.proc WHERE db='${DBNAME}' AND name='${SPNAME}'"
PROC_EXISTS=`mysql ${MYSQL_CONN} -ANe"${SQLSTMT}" | awk '{print $1}'`
if [ ${PROC_EXISTS} -eq 1 ]
then
SQLSTMT="SELECT type FROM mysql.proc WHERE db='${DBNAME}' AND name='${SPNAME}'"
PROC_TYPE=`mysql ${MYSQL_CONN} -ANe"${SQLSTMT}" | awk '{print $1}'`
SQLSTMT="SHOW CREATE ${PROC_TYPE} ${DBNAME}.${SPNAME}\G"
mysql ${MYSQL_CONN} -ANe"${SQLSTMT}" > ${SPFILE}
#
# Remove Top 3 Lines
#
LINECOUNT=`wc -l < ${SPFILE}`
(( LINECOUNT -= 3 ))
tail -${LINECOUNT} < ${SPFILE} > ${SPTEMP}
#
# Remove Bottom 3 Lines
#
LINECOUNT=`wc -l < ${SPTEMP}`
(( LINECOUNT -= 3 ))
head -${LINECOUNT} < ${SPTEMP} > ${SPFILE}
else
echo "Stored Procedure ${DBNAME}.${SPNAME} Does Not Exist"
fi
rm -f ${SPTEMP}
Here is a sample run:
[root@***]# ./GetMyProc.sh common DMSPushers
[root@***]# cat common_DMSPushers.sql
CREATE DEFINER=`pma`@`10.%` PROCEDURE `DMSPushers`()
BEGIN
DECLARE cntr INT DEFAULT 0;
DECLARE dealerName VARCHAR(50);
DECLARE dealerID INT;
DECLARE done boolean DEFAULT false;
DECLARE dealers CURSOR FOR SELECT bdd.dealer_id, ta.name FROM `b_dealer__dmsaccount` bdd left join t_dmsaccount ta on bdd.dmsaccount_id = ta.id where ta.dms_type = 1 order by bdd.dealer_id desc;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := true;
OPEN dealers;
read_loop: LOOP
FETCH dealers INTO dealerID, dealerName;
SET cntr = cntr + 1;
IF done OR cntr > 200 THEN
LEAVE read_loop;
END IF;
IF dealerID > 0 AND dealerID < 664 THEN
IF dealerName IS NULL THEN
SET dealerName = '';
END IF;
SET dealerName = REPLACE(dealerName,'''','''''');
-- Does the Database even exist???
SET @query = CONCAT('SELECT COUNT(SCHEMA_NAME) FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ''D',dealerID,''' INTO @schemaExists');
PREPARE stmt1 FROM @query;
EXECUTE stmt1;
IF @schemaExists > 0 THEN
SET @query = CONCAT('INSERT dealers_that_push (dealerID, dealerName, pushDate) SELECT ', dealerID, ',''', dealerName ,''',date FROM D',dealerID,'.contactLog where message like ''%pushed to the DMS%''');
PREPARE stmt1 FROM @query;
EXECUTE stmt1;
END IF;
END IF;
END LOOP;
CLOSE dealers;
END
[root@***]#
GREAT NEWS
Here is a script that will dump every Stored Procedure into separate SQL files:
MYSQL_CONN="-u... -p..."
SQLSTMT="SELECT COUNT(1) FROM mysql.proc"
PROCCOUNT=`mysql ${MYSQL_CONN} -ANe"${SQLSTMT}" | awk '{print $1}'`
if [ ${PROCCOUNT} -eq 0 ] ; then exit ; fi
SPLIST=""
for DBSP in `mysql ${MYSQL_CONN} -ANe"SELECT CONCAT(type,'@',db,'.',name) FROM mysql.proc"`
do
SPLIST="${SPLIST} ${DBSP}"
done
for TYPEDBSP in `echo "${SPLIST}"`
do
DB=`echo "${TYPEDBSP}" | sed 's/@/ /' | sed 's/\./ /' | awk '{print $2}'`
SP=`echo "${TYPEDBSP}" | sed 's/@/ /' | sed 's/\./ /' | awk '{print $3}'`
SQLSTMT=`echo "SHOW CREATE ${TYPEDBSP}\G" | sed 's/@/ /'`
SPFILE=${DB}_${SP}.sql
SPTEMP=${DB}_${SP}.tmp
echo Echoing ${SQLSTMT} into ${SPFILE}
mysql ${MYSQL_CONN} -ANe"${SQLSTMT}" > ${SPFILE}
#
# Remove Top 3 Lines
#
LINECOUNT=`wc -l < ${SPFILE}`
(( LINECOUNT -= 3 ))
tail -${LINECOUNT} < ${SPFILE} > ${SPTEMP}
#
# Remove Bottom 3 Lines
#
LINECOUNT=`wc -l < ${SPTEMP}`
(( LINECOUNT -= 3 ))
head -${LINECOUNT} < ${SPTEMP} > ${SPFILE}
rm -f ${SPTEMP}
done
ls -l
Give it a Try !!!
Best Answer
Although there are a number of tools out there, I prefer the command line for tasks such as this. Better performance, and just simpler.
Make sure you're logged into the mysql client:
The first step is to create a database in mysql that you want the data in:
Then you need to open--or use--that new database, which is empty:
All that's left is to do the import:
If all goes well, you'll see a bunch of output about "Query successful, N rows affected" and so on.