MySQL "commands out of sync" in phpmyadmin - mysql

I have just come across the MySQL Commands out of sync; you can't run this command now error. It's come up on SO before, but those questions and answers refer to application-level code.
Here is a file I'm trying to import through phpmyadmin. (It is simplified from the real code.) I am confused because I have other groups of drop/create/call statements in other files which imported fine.
What is causing the error?
DROP PROCEDURE IF EXISTS `c9gtd`.`select_changes`;
CREATE PROCEDURE `c9gtd`.`select_changes`
(
IN `user_id` INT,
IN `days` INT
)
SELECT
0 AS `id`
;
CALL `c9gtd`.`select_changes`(-1,-1); -- Test
DROP PROCEDURE IF EXISTS `c9gtd`.`select_info`;
CREATE PROCEDURE `c9gtd`.`select_info` (IN `id` INT) -- ERROR IS HERE
SELECT
0 AS `id`
;
CALL `c9gtd`.`select_info`(-1); -- Test

This is because both stored procedures are SELECTs. All the stored procedures up to this point have been UPDATEs: having multiple CALLs in one connection is ok when no data is returned.

Related

Azure Data Factory - Get SCOPE_IDENTITY value from MYSQL Stored Procedure

I'm using Lookup activity to invoke a MYSQL Stored Procedure. The requirement is to insert a record into a table, get the latest primary key and use that in another activity.
Below is the Stored Procedure
CREATE Procedure usp_LogAudit (
p_pipeline_id int,
p_status varchar(20)
)
begin
Insert into PipelineExecutionLog
(
pipeline_id,
execution_start,
execution_end,
status
)
values
(
p_pipeline_id ,
utc_timestamp(),
null,
p_status
);
SELECT LAST_INSERT_ID() AS SCOPE_IDENTITY;
end;
As, I'm using MYSQL, I don't have the facility to directly import the Stored Procedure along with it' parameters. (Note: For MS-SQL, it's straight forward)
As shown below, I'm using the Query option and invoking the MYSQL Stored Procedure using the 'call'.
Below is the Dynamic Expression
#concat('call usp_LogAudit(',variables('PipelineId'),',','''',variables('PipelineStatus'),'''',')')
The Lookup activity is calling the Stored Procedure successfully at database side. However, in ADF, it's throwing an error.
Not sure how to fix this. Any advice is appreciated.

Getting an error while trying to execute MYSQL stored procedure in phpmyadmin

I have created the following table:
CREATE TABLE Toy
(Toy_ID INT NOT NULL AUTO_INCREMENT,
Toy_Name VARCHAR(30) UNIQUE NOT NULL,
Toy_Price NUMERIC NOT NULL,
PRIMARY KEY (Toy_ID)
)
and then I inserted the values in toy table:
INSERT INTO Toy (Toy_Name,Toy_Price)
VALUES ('Naruto',25.00);
INSERT INTO Toy (Toy_Name,Toy_Price)
VALUES ('Goku',25.00);
INSERT INTO Toy (Toy_Name,Toy_Price)
VALUES ('Luffy',25.00);
and then I typed the following stored procedure in SQL window in phpmyadmin:
CREATE PROCEDURE searchtoy (IN toy_no INT)
BEGIN
SELECT * FROM Toy
WHERE Toy_ID = toy_no;
END;
The stored procedure has been created successfully.
Then I tried to execute the stored procedure in the SQL window and I also have added // in the Delimiter text box:
CALL searchtoy(1);
But I am getting the following error:
Error
Static analysis:
1 errors were found during analysis.
Unexpected token. (near ";" at position 17)
SQL query:
CALL searchtoy(1);
MySQL said: Documentation
#1305 - PROCEDURE demo.searchtoy does not exist
Despite the stored procedure being created successfully, it is still showing that the stored procedure does not exist.
Where did I go wrong ?
It would be really helpful if the solution code is provided.
it is looking for searchtoy in demo schema. check the schema in which you have created your function

Concurrent MYSQL procedure calls returning different results with an unrelated SELECT statement

I'm experiencing some very strange transactional behaviour in my MYSQL application.
I've managed to reduce the problem down to a small isolated test case, the code for which I’ve included below:
-- Setup a new environment
SET GLOBAL TRANSACTION ISOLATION LEVEL READ COMMITTED;
DROP DATABASE IF EXISTS `testDB`;
CREATE DATABASE `testDB`;
USE `testDB`;
-- Create a table I want two procedure calls to interact with
CREATE TABLE `tbl_test` (
`id` INT(10) UNSIGNED NOT NULL
, PRIMARY KEY (`id`)
);
-- A second table purely to demonstrate the issue
CREATE TABLE `tbl_test2` (
`id` INT(10) UNSIGNED NOT NULL
);
DELIMITER $$
DROP PROCEDURE IF EXISTS `sp_test` $$
CREATE PROCEDURE `sp_test` ()
BEGIN
START TRANSACTION;
-- CRAZY LINE
SELECT * FROM `tbl_test2`;
-- Insert ignore so both calls don’t try to insert the same row
INSERT IGNORE INTO `tbl_test` (`id`) VALUES (1);
-- Sleep added to make it possible to run concurrently manually
SELECT SLEEP(1) INTO #rubbish;
-- The result I am interested in
SELECT COUNT(*) FROM `tbl_test`;
COMMIT;
END $$
DELIMITER ;
Steps to Reproduce:
Run in the above script to create a test database, two tables and a stored procedure.
In two separate connections, as near to simultaneously as possible, run the stored procedures (you can increase the SLEEP time if you need longer):
USE `testDB`;
CALL sp_test ();
The Problem
When executed concurrently over two separate connections the SELECT COUNT(*) FROM `tbl_test`; statement returns different values for the two calls.
When I follow the steps above, I get back 1 from the first of the two procedure calls and 0 from the second.
My understanding of transactional behaviour and table locking is that when the first call reaches the INSERT statement it will create a lock. The second procedure call will reach the same line but must then wait until the transaction from the first call has been committed. Increasing the sleep time reinforces this idea as the second call will take twice as long to complete. If this is the case however, then the second procedure call should pick up the insert from the first call and both results should be equal to 1.
TL;DR
I'm expecting both to equal 1
Note that I am using READ_COMMITTED as my transaction isolation level.
I've tested this using MYSQL server and MariaDB
Further Weirdness
So at this point I assumed my understanding was incorrect. However, I then noticed that by removing the line SELECT * FROM `tbl_test2`; the results suddenly produced the expected values!
I've been experimenting with the script but essentially, including a SELECT statement to any table within the database before the INSERT line causes unanticipated results. I have absolutely no idea why this is the case.
Questions
Is my understanding of the expected transactional behaviour correct?
Why on earth does the SELECT statement to an unrelated table cause the transactional locking to fail?
If anyone can shed some light on this I would be very grateful!

Attempting to insert into a table then update a field within the same stored procedure

I'm attempting to create a procedure that is a basic insert into a table, and then performs a quick update on another table afterwards in MySQL. Please find the code below:
DROP PROCEDURE IF EXISTS `sp_insertUserSocial`
GO
CREATE PROCEDURE sp_insertUserSocial
(
IN p_userSocialID INT(11),
IN p_socialID INT(11),
IN p_userID INT(11),
IN p_referralID INT(11)
)
BEGIN
INSERT INTO UserSocial
(
userSocialID,
socialID,
userID,
referralID
)
VALUES
(
IN p_userSocialID,
IN p_socialID,
IN p_userID,
IN p_referralID
) ;
UPDATE Users
SET connCount = connCount + 1
WHERE UserID = p_referralID;
END
GO
In PHPAdmin it's giving me a syntax error, but I'm not sure where exactly it is? It says line 23, which makes me think it's the semi-colon but I thought that these were needed after an insert statement?
Any help is appreciated, thanks!
I see a couple of problems:
GO, must specify the DELIMITER. 19.1. Defining Stored Programs.
In the INSERT (13.2.5. INSERT Syntax), the IN is optional to pass parameters to the stored procedure (13.1.15. CREATE PROCEDURE and CREATE FUNCTION Syntax), but not part of the syntax of the INSERT.
SQL Fiddle demo

Executing MYSQL Stored Procedures using DBVisualizer

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.