How do I call stored procedure with JSON parameter in MySQL? - mysql

Here is the stored procedure and it's call
Stored Procedure (test)
BEGIN
DECLARE Query1 VARCHAR(500);
...
...
SET #Query1 = CONCAT('INSERT INTO tblName (col1, col2) values("',v_value1,'","',v_value2,'")'
);
PREPARE
stmt
FROM
#Query1;
EXECUTE
stmt;
Stored Procedure Call to test
CALL test( "abc",'{"pqr":true,"xyz":false}' );
When I try below then it's working fine but it's not working when I try to give parameters with above double quotes
CALL test( "abc","{'pqr':true,'xyz':false}" ); //Working fine
Error
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
near 'pqr":true,"xyz":false'

Even if it's a late answer...
It has to do with the way MySQL handles quotes
Whenever you include something into single quotes MySQL is going to treat it as a string.
For example:
"1" - could be treated as an integer if it is inserted into integer
field...
But
'1' - is going to be treated as a string no matter what.

Related

cannot insert datetime field with stored procedure into mysql database

I am getting this syntax error when trying to insert a datetime into mysql database.
pymysql.err.ProgrammingError: (1064, "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 '00:00:01)' at line 1")
I am using pymysql and flask-mysql. This is my code:
cursor.execute("""DROP TABLE IF EXISTS test_table_2;""")
cursor.execute("""DROP PROCEDURE IF EXISTS updateTestProc2;""")
testTable = """CREATE TABLE IF NOT EXISTS test_table_2 (
time DATETIME,
PRIMARY KEY (time)
);
"""
testProc = """
CREATE PROCEDURE updateTestProc2(
IN ptime DATETIME
)
BEGIN
SET #queryStr = CONCAT('INSERT INTO test_table_2(time) VALUES ( ',
ptime,
')
;'
);
PREPARE query FROM #queryStr;
EXECUTE query;
DEALLOCATE PREPARE query;
END
"""
cursor.execute(testTable)
cursor.execute(testProc)
proc_input = ('1990-05-23 00:00:01',)
cursor.callproc('updateTestProc2', proc_input)
Do not use string concatenation to get values into statement. That's error prone and might make your program vulnerable to SQL injection attacks. Use parameters. Change your procedure's code to:
...
SET #queryStr = 'INSERT INTO test_table_2 (time) VALUES (?)';
PREPARE query FROM #queryStr;
EXECUTE query USING ptime;
DEALLOCATE PREPARE query;
...
? is a parameter placeholder that is replaced with the value of the expression you pass with USING when you do EXECUTE. That way don't need to care about escaping, quoting etc. and can't do it wrong.

Error when executing a mysql store procedure

I have created a simple stored procedure that will drop a view. I am learning stored procedures in mysql so bear with me,
DELIMITER $$
CREATE PROCEDURE dropView (
IN viewName varchar(4000)
)
BEGIN
SET #sql:=CONCAT('DROP VIEW ',#viewName);
PREPARE dynamic_statement FROM #SQL;
EXECUTE dynamic_statement;
DEALLOCATE PREPARE dynamic_statement;
END$$
DELIMITER ;
And i am calling the above like so,
SET #theView = '`Report`;';
CALL dropView(#theView);
I am getting the error message,
Error Code: 1064. 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 'NULL' at line 1
I am using mysql 8.0.17
According to this i can use 'CREATE/DROP VIEW' in a prepared mysql.
#viewNameis a 9.4. User-Defined Variables and viewName one stored procedure parameter (13.1.17 CREATE PROCEDURE and CREATE FUNCTION Statements), are different variables.

Is it possible to copy mysql Routines to another Database using Editor

I have two databases Local and Development.
I want to move Local Db's procedures to development Db but not all. I only want newly created procedures.
For that I am writing code. But I stuck. I have definition but i don't know how to execute it on another Database using SQL Editor(Workbench/Heidi SQL).
Consider below example:
SET #Proc_new_procedure = '
DELIMITER $$
CREATE PROCEDURE `new_procedure` (input1 varchar(50),input2 varchar(50))
BEGIN
SET input1 = LTRIM(input1);
select * from table1 where column1 = input1;
select * from table2 where column1 = input2;
END$$
DELIMITER ;
';
PREPARE stmt1 FROM #Proc_new_procedure;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
I got below error:
Error Code: 1064. 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 DELIMITER $$ CREATE PROCEDURE new_procedure (input1 varchar(50),input2 varcha' at line 1
I tried Prepare statement to create procedure. But Prepare statement allow me only one statement execution at a time.
Can somebody please help me?

Mysql insert from stored procedure gives error 1064

For some strange reason, inserting from stored procedure is not working.
This is what Im trying to do:
DROP TABLE IF EXISTS test;
CREATE TABLE IF NOT EXISTS test(
id INT(9) NOT NULL AUTO_INCREMENT
,name VARCHAR(30) NOT NULL
,PRIMARY KEY (id)
) DEFAULT CHARSET=utf8;
insert into test (name) values('A');
Inserting from command line works with no problems.
Then I created a stored procedure to do the same kind of insert:
DROP PROCEDURE IF EXISTS storedtest;
DELIMITER $$
CREATE PROCEDURE storedtest()
BEGIN
declare insert_sql varchar(200);
SET insert_sql = 'insert into test (name) values(3)';
SELECT insert_sql;
PREPARE mystm FROM #insert_sql;
EXECUTE mystm;
END$$
DELIMITER ;
call storedtest();
This gives me the error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'NULL' at line 1
NULL? Where did NULL came from?
I also tried changing the sql-insert to look like this (dont know if it is a good way):
SET insert_sql = "insert into test (name) values('3')";
But mysql gives me exactly the same error.
Anyone has a clue?
The NULL MySQL is reporting is an empty user variable #insert_sql, which is different from the local stored procedure local variable insert_sql which you allocated with DECLARE.
MySQL's DECLARE is used for variables local to a stored program, but according to the documentation, PREPARE stmt FROM ... expects either a string literal or a user variable, which are the type preceded with #.
PREPARE stmt_name FROM preparable_stmt
preparable_stmt is either a string literal or a user variable that contains the text of the SQL statement.
You can allocate the untyped user variable with SET so there is no need for DECLARE. You may wish to set it to NULL when you're finished.
DROP PROCEDURE IF EXISTS storedtest;
DELIMITER $$
CREATE PROCEDURE storedtest()
BEGIN
-- Just SET the user variable
SET #insert_sql = 'insert into test (name) VALUES (3)';
SELECT #insert_sql;
-- Prepare & execute
PREPARE mystm FROM #insert_sql;
EXECUTE mystm;
-- Deallocate the statement and set the var to NULL
DEALLOCATE PREPARE mystm;
SET #insert_sql = NULL;
END$$
DELIMITER ;

mysql 5.1: how can i use benchmark() command to test a call to a stored procedure?

I'm trying to benchmark a stored procedure.
select benchmark(100000000,(select 1));
this benchmark works
but the following benchmark doesn't:
do benchmark(1000,(call test_login_user('a')));
it produces the following error:
ERROR 1064 (42000): 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 xpofb_login_user('a')))' at line 1
any ideas how to resolve the issue ?
You can't do this with benchmark(), but you could create a stored procedure to do it.
Here's an example:
delimiter $$
create procedure benchmark_test_login_user (p_username varchar(100),
p_count int unsigned)
begin
declare v_iter int unsigned;
set v_iter = 0;
while v_iter < p_count
do
call test_login_user(p_username);
set v_iter = v_iter + 1;
end while;
end $$
delimiter ;
call benchmark_test_login_user('a',1000);
You can't
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_benchmark
Only scalar expressions can be used. Although the expression can be a subquery, it must return a single column and at most a single row. For example, BENCHMARK(10, (SELECT * FROM t)) will fail if the table t has more than one column or more than one row.