I'm new to SPs and using Adminer
SET #startDate = initDate;
SET #startTime = initTime;
My stored proc begins like this but throws an error when there is more that one line. initDate and initTime are input vars
It looks like some sort of issue with the delimiter as if both assignments go on one line with a , separator the error shifts further in to the SP.
Error message
Syntax error near 'SET #startTime = initTime' at line 4 13:32:48
Would appreciate any tips
Normally when defining a stored procedure, you re-define the delimiter beforehand:
DELIMITER $$
CREATE PROCEDURE . . .
BEGIN
SET #startDate = initDate;
SET #startTime = initTime;
. . .
END$$
DELIMITER ;
It does sound like you might have a problem with the delimiter. I also advise you to wrap the body in BEGIN/END.
Related
I've spent hours and I can't understand why this is highlighted red and I don't know what my error is.
CREATE FUNCTION Student_Section_Count(StudentID INT)
Returns INT
Begin
DECLARE section_Registred INT;
SET section_Registred= (Select COUNT(DISTINCT Section.ID) as 'Students Registration Count'
FROM Section
Inner Join
Registration on registration.StudentID=Section.ID
Where registration.StudentID=StudentID);
Return Section_Registred;
END$$
Delimiter;
It highlights END and Delimiter, as well as INT from Return INT
By default semi-colon ; is the delimiter in mysql. So when using a mysql client, the function definition that is passed to server is only up to the first semi-colon i.e. DECLARE section_Registred INT;. Rest of the definition of function is not passed to server. This produces an error. To incorporate semi-colon in function definition you need to change your delimiter from semi-colon to some thing else. So before defining the function write the below sql to change the delimiter:
DELIMITER $$.
After the above sql write sql for your function. Append the new delimiter $$ to END at the end of definition of your function. Now the whole definition of function is passed to server up to the new delimiter. This will fix your error. Change the delimiter back to default after your function definition ends as below:
DELIMITER ;.
Also remember to choose a delimiter that is not present anywhere inside your function definition.
Well, I guess I figured it out on my own.
DELIMITER $$
CREATE FUNCTION Student_Section_Count(StudentID INT)
Returns INT
Begin
DECLARE section_Registred INT;
SET section_Registred= (Select COUNT(DISTINCT Section.ID) as 'Students Registration Count'
FROM Section
Inner Join
Registration on registration.StudentID=Section.ID
Where registration.StudentID=StudentID);
Return Section_Registred;
END$$
I have been trying to use a cursor and following a tutorial I found online, I was able to come up with the following cursor.
DELIMITER $$
CREATE PROCEDURE customers_with_oldest_version (INOUT customerCount varchar(4000))
BEGIN
DEClARE customers_with_oldest_version CURSOR FOR
select * from CustomerSoftware where software in (select min(minimumSoftware) from ProductSoftware);
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET #finished = 1;
set #row_entry = "";
open customers_with_oldest_version;
get_customers: LOOP
FETCH customers_with_oldest_version INTO #row_entry;
IF #finished = 1 THEN
LEAVE get_customers;
END IF;
SET #customerCount = #customerCount + 1;
END LOOP;
CLOSE customers_with_oldest_version;
END$$
DELIMITER ;
But I am unable to create this procedure, since phpmyadmin is giving me an error saying that
#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 '#row_entry; IF #finished = 1 THEN LEAVE get_customers; END IF; SET #c' at line 16
What am I missing here?
You can't fetch into a user-defined variable. This is a bug that has been around for many years, see https://bugs.mysql.com/bug.php?id=2261
Declare a regular local variable for it.
I am trying to use Database trigger and procedure to send sms I have create the table and I can insert data by:
use sms;
INSERT INTO sms_out (id, sender,receiver,msg)
VALUES ('','MyName','25578200000',"test");
I have created trigger as;
use sms;
CREATE TRIGGER push_message_trigger AFTER INSERT ON sms_out
FOR EACH ROW
CALL push_message(NEW.sender, NEW.receiver, NEW.msg);
and I have created procedure as;
use sms;
DELIMITER $$
CREATE PROCEDURE push_message
(p1 TEXT,
p2 DOUBLE,
p3 TEXT)
BEGIN
DECLARE cmd CHAR(255);
DECLARE result CHAR(255);
SET cmd = CONCAT('curl "http://www.sms.co.tz/api.php?do=sms&username=Myusername&password=MyPassword&senderid=MyName&dest=25571500000&msg=test"');
SET result = sys_eval(cmd);
END$$;
The above does not work but if I replace
SET cmd = CONCAT('curl "http://www.sms.co.tz/api.php?do=sms&username=Myusername&password=MyPassword&senderid=MyName&dest=25571500000&msg=test"');
With
SET cmd = CONCAT('touch /var/lib/mysql/Advo');
It works!!
Whats wrong with curl??
I guess the problem it's not the curl literal is how are you using mysql CONCAT funcion.
To concat strings you should do something like (if you don't use commas you aren't concatening strings):
CONCAT(str1, str2, ....);
Seems that the problem are the quotes, try to escape the double quotes like this \"
* Show the error if you want people to know what's really happening
I have made an upgrade for my MySQL Server to 5.1.39 and now when I run SQL scripts (which had worked previously) - it throws error. I have checked syntax many times and I couldn't find any incompatible code parts. Please suggest any solution for this problem.
Error message
Mysql::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 'CREATE FUNCTION clean_dymmy_table (dummy_name VARCHAR(255)) RETURNS V' at line 3:
SQL code:
/*DELIMITER //*/
DROP FUNCTION IF EXISTS clean_dymmy_table;
CREATE FUNCTION clean_dymmy_table (dummy_name VARCHAR(255)) RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
DECLARE temp_val VARCHAR(255);
SET temp_val = dummy_name;
-- Test
SET temp_val = REPLACE(temp_val, 'Tmp ', '');
SET temp_val = REPLACE(temp_val, ' TmP', '');
SET temp_val = REPLACE(temp_val, 'TMP ', '');
SET temp_val = REPLACE(temp_val, ' TMP', '');
SET temp_val = REPLACE(temp_val, ' tmp', '');
RETURN dummy_name;
END/*//*/
Not sure why you removed the DELIMITER part, but when I add that back in, it runs fine:
DELIMITER // -- you have to change what MySQL expects between commands
DROP FUNCTION IF EXISTS clean_dymmy_table // -- tell it a new command's coming
CREATE FUNCTION clean_dymmy_table (dummy_name VARCHAR(255))
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
-- now this can be parsed as part of the current command.
DECLARE temp_val VARCHAR(255);
SET temp_val = dummy_name;
-- Test
RETURN dummy_name;
END
// -- Now you're done with that command.
-- go back to semi-colons, because otherwise life is too zany for me.
DELIMITER ;
(This was in 5.1.54... but I don't think that should matter)
A hunch: There seems to be a MySQL Bug in the 5.1.x line pertaining to DELIMITER that may be biting you here:
#46429 use DELIMITER command in MySql.Data.MySqlClient.MySqlScript
Though that heavily depends on how you're calling it, given cwallenpoole's response, your symptom does suggest it may well be that bug, given your MySQL version. Is upgrading a possibility for you?
delimiter //
CREATE DEFINER=root#localhost PROCEDUREgetData(IN templateName VARCHAR(45),IN templateVersion VARCHAR(45),IN userId VARCHAR(45))
BEGIN
set #version = CONCAT("SELECT saveOEMsData_answersVersion FROMsaveOEMsData WHERE saveOEMsData_templateName = '",templateName,"' ANDsaveOEMsData_templateVersion = ",templateVersion," AND saveOEMsData_userId= ",userId);
PREPARE s1 from #version;
EXECUTE S1;
END // delimiter ;
I am retrieving saveOEMsData_answersVersion, but I have to use it in an IF loop, as in if the version == 1, then I would use a query, else I would use something else. But I am not able to use the version. Could someone help with this?? I am only able to print but not able to use the version for data manipulation. The procedure works fine but I am unable to proceed to next step which is the if condition.
The if condition would have something like the below mentioned.
IF(ver == 1) THEN SELECT "1";
END IF;
IF is allowed inside a Procedure, you can not use IF inside SQL, in SQL you can use CASE, but that doesn't have the same abilities.
Build a second procedure to handle the IF/Then for the results of getData