EXISTS and output parameter in stored procedure in mysql - mysql

Here is my stored procedure for returning true if any data is found in document details but it generates an error
" #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 '#result = true end if end' at line 5"
create procedure abcde(out result boolean)
begin
if exists(select * from document_details)
then #result = true;
end if
end
Kindly,provide me a solution asap

Besides the fact that you need to use SET to assign a value to a local variable or a parameter, there are other things worth mentioning
you have to have a semicolon after END IF
since EXSITS() returns BOOLEAN you can just directly assign it to your OUT parameter
That being said your stored procedure might look like
CREATE PROCEDURE abcde(OUT result BOOLEAN)
SET result =
(
EXISTS(SELECT *
FROM document_details)
);
Note: now it's just one statement. Therefore you don't even need to change DELIMITER and use BEGIN ... END block.
Here is SQLFiddle demo

Related

Trying create multuple table using LIKE from multiple tables as source using arrays

I have two base tables "tutorials_tb2","tutorials_tbl"
and I am trying to create two new tables from these tables as "tutorials_tb2_new","tutorials_tbl_new"
So tutorials_tb2 will be source to tutorials_tb2_new and similarly tutorials_tbl to tutorials_tbl_new
I am trying by creating a MySQL function with new and orig tables as array by using JSON_* methods
DELIMITER //
CREATE FUNCTION example()
BEGIN
DECLARE _counter INT DEFAULT 0;
DECLARE _value varchar(50);
SET #origTables = '["tutorials_tb2","tutorials_tbl"]';
SET #newTables = '["tutorials_tb2_new","tutorials_tbl_new"]';
WHILE _counter < JSON_LENGTH(#origTables) DO
CREATE TABLE JSON_VALUE(#newTables, CONCAT('$[',_counter,']')) LIKE JSON_VALUE(#origTables, CONCAT('$[',_counter,']'))
SET _counter = _counter + 1;
END WHILE;
END //
DELIMITER ;
SELECT example();
And while executing I am getting below 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 'BEGIN
DECLARE _counter INT DEFAULT 0;
DECLARE _value varchar(50);
SET #origT' at line 2
MySQL>
The reason for your syntax error is that a stored function requires a RETURNS <type> clause before the BEGIN. Review the syntax documentation: https://dev.mysql.com/doc/refman/8.0/en/create-procedure.html
The next problem you will encounter, as Akina commented above, is that the JSON expression returns a string, not an identifier. You can't CREATE TABLE <string-expression> and have the result of the expression be the table name.
You would have to interpolate the string expression into a string, and execute that string as a dynamic SQL statement. But you can't do that in a stored function.
https://dev.mysql.com/doc/refman/8.0/en/stored-program-restrictions.html#stored-routine-sql-restrictions says:
SQL prepared statements (PREPARE, EXECUTE, DEALLOCATE PREPARE) can be used in stored procedures, but not stored functions or triggers. Thus, stored functions and triggers cannot use dynamic SQL (where you construct statements as strings and then execute them).
Really, you are making this task much harder than it should be by using stored functions and JSON.
Just write an app or a script that loops over the table names you want to create, format the DDL statement as a string including the table name, then execute the DDL statement.
Here's an example in Python:
import mysql.connector
cnx = mysql.connector.connect(...)
cursor = cnx.cursor()
for tablename in ["tutorials_tb2","tutorials_tbl"]:
sql = f"CREATE TABLE `{tablename}_new` LIKE `{tablename}`"
cursor.execute(sql)
cnx.close()
I hardly ever use stored routines or JSON in MySQL.

Simple stored procedure parameter is causing an unknown error

It's a simple procedure, I just cannot for the life of me find the error in this syntax. I've learned at other posts and couldn't find a working solution.
CREATE PROCEDURE cooldownUpdate #command VARCHAR(18)
AS
UPDATE `mention_cooldowns` SET cooldown = 'true' WHERE command = #command
GO;
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 '#command VARCHAR(18) AS UPDATE mention_cooldowns SET cooldown = 'true' WHERE c' at
line 1
Firstly, you have to set a delimiter while creating stored procedure. Also, you have to define whether the parameter is for IN or OUT and finally you have to wrap your queries inside BEGIN and END for best practice.
Please check below for creating stored procedure.
DELIMITER //
CREATE PROCEDURE cooldownUpdate(IN pCommand VARCHAR(18))
BEGIN
UPDATE `mention_cooldowns` SET cooldown = 'true' WHERE command = pCommand;
END //
DELIMITER ;

#1064 Error SQL - Happens on all my procedures/functions

I know this question has been asked a bunch in different forms, but none of the ones I looked at (quite a bit) seemed to help me out in my specific case. I wrote a few functions and procedures and I always get the same error at the same spot. Here is my code:
DELIMITER |
DROP PROCEDURE IF EXISTS STUDENTS_BY_STATUS;
CREATE PROCEDURE STUDENTS_BY_STATUS (sts VARCHAR(10))
BEGIN
SELECT BannerId, Name FROM STUDENT WHERE Status = sts;
END |
DELIMITER;
This happens on all my procedures functions, this error:
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 'CREATE PROCEDURE STUDENTS_BY_STATUS (sts VARCHAR(10))
BEGIN
SELECT BannerId,' at line 2
On my other one its this:
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 'CREATE FUNCTION GoodGrade(letGrade VARCHAR(2)) RETURNS int
BEGIN
DECLARE v' at line 2
This happens whether or not I use | or // as the DELIMITER...can someone tell me what I'm doing wrong here? Thanks!
You've changed the delimiter, written a statement and then not delimited it according to your new definition
Change the line
DROP PROCEDURE IF EXISTS STUDENTS_BY_STATUS;
to
DROP PROCEDURE IF EXISTS STUDENTS_BY_STATUS |
Update
This wasn't obvious from the initial error message, but you have another error when you reset the Delimiter after the procedure definition. You need a space before the ';' on that one.
so change
DELIMITER;
to
DELIMITER ;
NB
The other use of ';' within your procedure definition is correct, because you want everything within the create statement to to be processed together.

Using procedure variables to create server-side prepared statements — MySQL

Within procedures, why does using session-specific variables to store string queries work:
DELIMITER $$
CREATE PROCEDURE `test_prepared_stmt` () BEGIN
SET #q:="SELECT * FROM t1";
PREPARE query FROM #q;
#...
END$$
but using procedure variables not:
DELIMITER $$
CREATE PROCEDURE `test_prepared_stmt` () BEGIN
DECLARE q TEXT;
SET q:="SELECT * FROM t1";
PREPARE query FROM q;
#...
END$$
and instead throw 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 'q;
END' at line 5
Is this just a quirk of the language?
You need to use SQL standard variable syntax - all variables are declared/used with the # symbol.
So, in your example, if you used:
declare #q text
or,
declare #q nvarchar(4000)
or anything similar, it will work - just follow the SQL spec for usage of variables and you should be fine, regardless of whether you are within a procedure or passing a parameter, or anything else.

Can't create MySQL procedure to update table

I am trying to make a procedure to update an existing user. It receives the name of the user and then increments his points column. I have it like this:
CREATE PROCEDURE addPoints (IN nomeus varchar(20))
BEGIN
UPDATE User
SET
points=points+1
WHERE (nome=nomeus) ;
END;
However, I get this 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 '' at line 8
How can I fix it?
When in doubt just go to dev.mysql they have some great documentation there.
http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html
Looks like you found the answer to your own question. However, if you change any of the code in the procedure, don't forget to drop the procedure before trying to create it again.
Drop Procedure if exists addPoints;
http://dev.mysql.com/doc/refman/5.0/en/drop-procedure.html
Yeah, I had to use delimiters:
delimiter //
CREATE PROCEDURE addPoints(IN nomeuser varchar(256))
BEGIN
UPDATE User
SET
points = points + 1
WHERE nome = nomeuser;
END;//
delimiter ;