Does SQL-fiddle facilitate execution of triggers/stored procedures?
I have been unable to execute even the simplest form of stored procedure on sqlfiddle
DELIMITER $$
DROP PROCEDURE IF EXISTS myProc $$
CREATE PROCEDURE myProc()
BEGIN
END$$
DELIMITER ;
Sqlfiddle does not allow executing this(above) sql in build schema, but allows create table etc
Note: The same syntax is working for me on my localhost using wamp with mysql 5.5.24
Can anyone guide please?
Instead of using the delimiter option (which is not a real SQL statement, but rather only a command for the mysql command prompt) use the "Query Terminator" option on SQL Fiddle to establish your delimiter.
For example:
http://sqlfiddle.com/#!2/88fcf
Note the // dropdown below the schema box? That's the SQL Fiddle equivalent to the mysql DELIMITER command.
Longer example with queries in the stored procedure (note that within the stored procedure, ; is still used as a delimiter):
http://sqlfiddle.com/#!9/4db78
Full disclosure: I'm the author of SQL Fiddle.
I couldn't get this answer to work on sql fiddle, but found db-fiddle, and it seems to work.
Example in DB Fiddle
If the above doesn't work for some reason, do the following
Go here: https://www.db-fiddle.com/
Enter the following SQL on the left, and SELECT * FROM tblTest; on the right.
Select "MySql 5.7" or whatever in dropdown.
Click "Run"
DELIMITER //
CREATE TABLE tblTest (col1 INT)//
INSERT INTO tblTest VALUES (9)//
CREATE PROCEDURE dowhile()
BEGIN
DECLARE v1 INT DEFAULT 3;
WHILE v1 > 0 DO
INSERT INTO tblTest VALUES(v1);
SET v1 = v1 - 1;
END WHILE;
END//
INSERT INTO tblTest VALUES (8)//
select * from tblTest//
call dowhile()//
select * from tblTest//
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`()
one Stored Procedure is returning me a table and I want to hold that result into another Stored Procedure.
and I am facing SQL syntax error.
First Stored Procedure
DELIMITER $$
USE `dataBase`$$
DROP PROCEDURE IF EXISTS `testReturnTable`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `testReturnTable`()
BEGIN
SELECT `user_id`, `email` FROM `users`;
END$$
DELIMITER ;
and I am trying to call this Store Procedure into another and want to hold the data into a view or table
DELIMITER $$
USE `dataBase`$$
DROP PROCEDURE IF EXISTS `testReturnCall`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `testReturnCall`()
BEGIN
DROP VIEW IF EXISTS `my_view`;
CREATE OR REPLACE VIEW my_view AS (CALL testReturnTable());
SELECT * FROM my_view;
END$$
DELIMITER ;
and getting error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CALL testReturnTable());
I can use function here as well, but I don't know how to handle result set,
please advice.
it is not possible to get table ouput from one stored procedure to another stored procedure. But we have alternate to do this which is Temp table
DELIMITER $
DROP PROCEDURE IF EXISTS CREATE_BACKUP$
CREATE PROCEDURE CREATE_BACKUP()
BEGIN
DECLARE BACK INT DEFAULT 0;
SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'STUDENTDB'
;
SHOW_LOOP:LOOP
IF BACK = 1
THEN
LEAVE SHOW_LOOP;
END IF;
CREATE TABLE STUDENT_BACKUP
AS SELECT * FROM STUDENT;
CREATE TABLE SCORE_BACKUP
AS SELECT * FROM SCORE;
CREATE TABLE GRADE_EVENT_BACKUP
AS SELECT * FROM grade_event;
END LOOP SHOW_LOOP;
END$
DELIMITER ;
Hi, when I run this procedure, it runs more than one time. So I get an error which says "STUDENT_BACKUP table already exists" for the second time when it runs. What should I do to run it just 1 time?
In MySQL you can use CREATE TABLE IF NOT EXIST... to avoid the error occurrence. See CREATE TABLE syntax for details.
To solve your quesrion for SQL server use an INFORMATION_SCHEMA view. A similar solution is in the existing topic.
I come from a background with Microsoft SQl using their Server management studio. I have recently switched to mysql and am looking to create a stored procedure with the same method I use with MSSQL. I want to create a procedure if it does not exists because I prefer that to dropping if exists. Below is the syntax I would use in MSSQL. Any help would be much appreciated.
IF NOT EXISTS
(
SELECT
1
FROM
sysobjects WITH (NOLOCK)
WHERE
[type] = 'P' AND name = 'Sproc'
)
EXEC('CREATE PROCEDURE dbo.Sproc AS BEGIN SET NOCOUNT ON; END')
GO
ALTER PROCEDURE dbo.Sproc
(
#Sproc_Params
)
AS
BEGIN
.... --Sproc code
END
You can look in mysql.proc to see if a procedure exists.
SELECT db, name FROM mysql.proc WHERE db = 'dbo' AND name = 'Sproc';
However, in MySQL, you cannot ALTER PROCEDURE to replace the procedure body. You can only change a few attributes of the procedure. See http://dev.mysql.com/doc/refman/5.7/en/alter-procedure.html for details.
So you will have to drop and create the procedure anyway, if you want to change its parameters or its body.
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.