I am trying to create a simple stored procedure in phpmyadmin on namecheap.com server. I have created this procedure on my localhost and there was no problem however I'm unable to understand why is it not being created now.
I have tried by using mySQL as well as from Add Routine option.
My table name is users and here is the stored procedure
CREATE PROCEDURE getUsers (myUsername varchar (32))
BEGIN
SELECT * FROM `users` WHERE username = myUsername;
END$$
However I have another table called albums and I've written similar procedure for that table and it is working fine. Please help
Stucks on Loading Screen
You need the DELIMITER and IN for the params:
DELIMITER //
CREATE PROCEDURE getUsers (IN myUsername varchar (32))
BEGIN
SELECT * FROM `users` WHERE username = myUsername;
END //
DELIMITER ;
Related
I'm trying out MySQL procedures for the first time, however I can't figure out how to define the variable #index_ids for the life of me. It really doesn't like the SET.
CREATE PROCEDURE #indextemp
BEGIN
SET #index_ids = (SELECT DISTINCT index_id FROM visibility_index_processing_queue WHERE process_id IS NOT NULL);
SELECT #index_ids;
END
Problem is in CREATE PROCEDURE syntax, not in setting variable. You just have to add parentheses after procedure name. Here's working sample
delimiter $
CREATE PROCEDURE indextemp()
BEGIN
SET #index_ids = (SELECT DISTINCT index_id FROM visibility_index_processing_queue WHERE process_id IS NOT NULL);
SELECT #index_ids;
END$
delimiter ;
Sometimes use of delimiter character in procedure body can cause problems too. That's why I set delimiter to $ before creating procedure and revert it to default ; after I'm done.
Also notice that I have removed # from your procedure name. In sql # is used to insert comments. If for some reason you really want to use it in your name you have to do it like that
CREATE PROCEDURE `#indextemp`()
I'm quite new to stored procedures in mySQL and I have been having trouble with some of my homework that I have been assigned.
It's asking me to accept cust_code as a procedure parameter in my database, and then it will delete said customer with the cust_code.
Then it will delete all associated rows with this customer in the tables 'Invoice', 'line'.
To create a procedure with a integer parameter do something like this:
DELIMITER $$
CREATE PROCEDURE `<procedure_name>`(IN `in_cust_code` INT)
BEGIN
DELETE
...
WHERE 1
AND `<table_name>`.`cust_code` = in_cust_code
...
END
DELIMITER ;
Just wondering, im new to stored procedures,
I just tried this
INSERT INTO new.emp SELECT * FROM old.users
and it worked and inserted the data of users from the old table users to the new emp table.
But when I did that inside a stored procedure im getting a syntax error
CREATE PROCEDURE insertnew
INSERT INTO new.emp SELECT * FROM old.users
What's the difference?
In what part of the query are you getting a syntax error? The correct syntax for creating a stored procedure is something like this:
DELIMITER //
CREATE PROCEDURE InsertEmployee()
BEGIN
INSERT INTO new.emp SELECT * FROM old.users;
END //
DELIMITER ;
I am using DBVisualizer for the first time. I have made a stored procedure in mysql database. However I am unable to execute it from DBVisualizer.
This is how the procedure looks like. Here // is used as delimiter. I have four columns in the table namely slno int (autoincrement), time timestamp, price int, and prodid varchar.
*DROP PROCEDURE `spTest`//
CREATE DEFINER=`root`#`localhost` PROCEDURE `spTest`()
BEGIN
SELECT * FROM dummy1 where prodid=pr01;
END*
In DBVisualizer, I am executing #call spTest()
Where am I going wrong?
As the definer of the stored procedure is root, your DBVizualizer connection to MySQL must be as the root user in order to execute it.
I want to know how to use DROP TABLE IF EXISTS in a MySQLstored procedure.
I'm writing a rather long mySQL Stored Procedure that will do a bunch of work and then load up a temp table with the results. However, I am having trouble making this work.
I've seen a few ways to do the temp table thing. Basically, you either create the temp table, work on it, and then drop it at the end ... or you drop it if it exists, create it, and then do your work on it.
I prefer the second method so that you always start of clean, and it's a built-in check for the table's existence. However, I can't seem to get it to work:
Here are my examples:
This Works:
DELIMITER//
DROP PROCEDURE IF EXISTS pTest//
CREATE PROCEDURE pTest()
BEGIN
CREATE TEMPORARY TABLE tblTest (
OrderDate varchar(200)
);
DROP TEMPORARY TABLE tblTest;
END//
DELIMITER ;
CALL pTest();
This Works:
DELIMITER//
DROP PROCEDURE IF EXISTS pTest//
CREATE PROCEDURE pTest()
BEGIN
DROP TEMPORARY TABLE tblTest;
CREATE TEMPORARY TABLE tblTest (
OrderDate varchar(200)
);
END//
DELIMITER ;
CALL pTest();
This does not:
DELIMITER//
DROP PROCEDURE IF EXISTS pTest//
CREATE PROCEDURE pTest()
BEGIN
DROP TEMPORARY TABLE IF EXISTS tblTest;
CREATE TEMPORARY TABLE tblTest (
OrderDate varchar(200)
);
END//
DELIMITER ;
CALL pTest();
The first 2 work, but if that table exists (like if the procedure didn't finish or something), it'll obviously end with a "Table tblTest does not exist" error. The non-working example is what I'm looking for -- drop the table if its there and then recreate it so that I can start clean.
It feels like it's the "IF EXISTS" making this thing fail. I've copied code from all sorts of sites that do things very similar and in no case can I get a "DROP TABLE IF EXISTS..." to work. Ever.
Dev Server: mySQL Server version: 5.1.47-community
Prod Server: mySQL Server version: 5.0.45-log
We can't change db versions (DBAs won't allow it), so I'm stuck on what I have. Is this a bug in mySQL or in the Procedure?
Thanks.
It's an old question but it came up as I was looking for DROP TABLE IF EXISTS.
Your non-working code did not work on my MySQL 5.1.70 server.
All I had to do was add a space between DELIMITER and // on the first line, and everything worked fine.
Working code:
DELIMITER //
DROP PROCEDURE IF EXISTS pTest//
CREATE PROCEDURE pTest()
BEGIN
DROP TEMPORARY TABLE IF EXISTS tblTest;
CREATE TEMPORARY TABLE tblTest (
OrderDate varchar(200)
);
END//
DELIMITER ;
I don't know why this is not working for you,but you should be able to work around the issue using a continue handler. If you put the DROP TABLE statement into it's own BEGIN...END block you can use a continue handler to ignore the error if the table does not exist.
Try this:
DELIMITER //
DROP PROCEDURE IF EXISTS pTest //
CREATE PROCEDURE pTest()
BEGIN
BEGIN
DECLARE CONTINUE HANDLER FOR SQLSTATE '42S02' BEGIN END;
DROP TEMPORARY TABLE tblTest;
END;
CREATE TEMPORARY TABLE tblTest (
OrderDate varchar(200)
);
END //
DELIMITER ;
CALL pTest();
I also had the same problem. It seems MySQL doesn't like to check if the table exists on some versions or something. I worked around the issue by querying the database first, and if I found a table I dropped it. Using PHP:
$q = #mysql_query("SELECT * FROM `$name`");
if ($q){
$q = mysql_query("DROP TABLE `$name`");
if(!$q) die('e: Could not drop the table '.mysql_error());
}
You suppress the error in the first query with the # symbol, so you don't have an interfering error, and then drop the table when the query returns false.
I recommend to add new line
SET sql_notes = 0// before DROP PROCEDURE IF EXISTS get_table //
Otherwise it will show warning PROCEDURE does not exists.