DELIMITER $$
drop procedure IF EXISTS `simpleproc`$$
CREATE PROCEDURE `simpleproc`(OUT param1 INT)
BEGIN
SELECT COUNT(*) INTO param1 FROM t;
END $$
DELIMITER ;
I am trying to import the above code using PHP but I'm getting this error:
Error performing query '':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 $$ drop procedure IF EXISTS simpleproc$$ CREATE PROCEDURE `simplepro' at line 1
DELIMITER $$
DROP PROCEDURE IF EXISTS `simpleproc`$$
CREATE PROCEDURE `simpleproc`(OUT param1 INT)
READS SQL DATA
BEGIN
SELECT COUNT(*) INTO param1 FROM t;
END $$
DELIMITER ;$$
The above syntax can only be executable at MySQL command line client.
The same won't work using server side scripting language.
Instead you can use the following as a string to construct the query and pass to mysqli->query
CREATE PROCEDURE simpleproc (OUT param1 INT )
BEGIN
SELECT COUNT(*) INTO param1 FROM t;
END;
Note:
If you have only a single statement to be processed and executed in a stored procedure,
you don't even need begin - end blocks.
This is correct:
CREATE PROCEDURE simpleproc (OUT param1 INT )
SELECT COUNT(*) INTO param1 FROM t;
You have an error in your procedure syntax, please use following
DELIMITER $$;
DROP PROCEDURE IF EXISTS `simpleproc`$$
CREATE PROCEDURE `simpleproc` (OUT param1 INT)
BEGIN
SELECT COUNT(*) INTO param1 FROM t;
END$$
DELIMITER ;$$
This will solve your problem
Related
I'm trying to create this trigger but i'm getting
[Err] 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 TRIGGER ca_passwd_trigger BEFORE UPDATE ON users
FOR EACH ROW BEGIN ' at line 1
delimiter $$
DROP TRIGGER IF EXISTS ca_passwd_trigger ;
$$
CREATE TRIGGER ca_passwd_trigger BEFORE UPDATE ON users
FOR EACH ROW
BEGIN
IF ((NEW.passwd <=> OLD.passwd) = 0) THEN
SET NEW.passwd_modified_at = NOW();
END IF;
END;$$
delimiter ;
In your query you added the query terminator ; with the delimiter $$ in two places. The below query is having proper query terminators and delimiters.
DELIMITER $$
DROP TRIGGER IF EXISTS ca_passwd_trigger; -- removed the delimiter $$ after the terminator ;
CREATE TRIGGER ca_passwd_trigger BEFORE UPDATE ON users
FOR EACH ROW
BEGIN
IF ((NEW.passwd <=> OLD.passwd) = 0) THEN
SET NEW.passwd_modified_at = NOW();
END IF;
END$$ -- removed the terminator ; before the delimiter $$
DELIMITER ;
For a legacy project, I would like to execute this query using CodeIgniter :
DELIMITER $$
CREATE FUNCTION `getCustomerFullName`(intCustomerID INT)
RETURNS varchar(100) CHARSET latin1
return CONCAT(
(SELECT FirstName FROM Customer WHERE CustomerID = intCustomerID),
' ',
(SELECT LastName FROM Customer WHERE CustomerID = intCustomerID))$$
DELIMITER ;
When I try to use $this->db->query(), I get this 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
'DELIMITER $$ CREATE FUNCTION `getCustomerFullName`(intCustomerID INT) RETURNS '
at line 1
How can I execute such "multi-line" query using CodeIgniter ?
The issue is in the query, not CodeIgniter. Use the regular delimiter inside your function:
DELIMITER $$
CREATE FUNCTION `getCustomerFullName`(intCustomerID INT)
RETURNS varchar(100) CHARSET latin1
return CONCAT(
(SELECT FirstName FROM Customer WHERE CustomerID = intCustomerID),
' ',
(SELECT LastName FROM Customer WHERE CustomerID = intCustomerID));
$$
DELIMITER ;
if you want to execute a Function or SP, you need to add this to mysql (in the mtop menu of your phpmyadmin or Workbench) you have many otions
in More (routine / new / add new)
create a function or sp
Then in CI you only call like
$this->db->query('call Function()');
Best solution i get was looking inside system libs and use it.
$this->db->simple_query("YOUR FULL SQL STRING");
I was looking on google and not solution provided.
I try to make loop in MySQL, but it always shows me an error.
Is it correct code or did I make a mistake?
DROP PROCEDURE IF EXISTS proc_loop_test;
CREATE PROCEDURE proc_loop_test()
BEGIN
DECLARE int_val INT DEFAULT 0;
test_loop : LOOP
IF (int_val = 10) THEN
LEAVE test_loop;
END IF;
SET int_val = int_val +1;
SELECT int_val;
END LOOP;
END;
#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 '' at line 3
The problem is that the default delimiter is ; and stops the execution/parsing of the procedure code. Change the delimiter before you create the procedure and set it back to ; after the procedure code like this:
DROP PROCEDURE IF EXISTS proc_loop_test;
DELIMITER #
CREATE PROCEDURE proc_loop_test()
BEGIN
...
END #
DELIMITER ;
I'm trying to create a function in MySQL workbench, I have this SQL code:
DELIMITER $$
CREATE FUNCTION `regex_replace` (pattern VARCHAR(1000),replacement VARCHAR(1000),original VARCHAR(1000))
RETURNS VARCHAR(1000)
DETERMINISTIC
BEGIN
DECLARE temp VARCHAR(1000);
DECLARE ch VARCHAR(1);
DECLARE i INT;
SET i = 1;
SET temp = '';
IF original REGEXP pattern THEN
loop_label: LOOP
IF i>CHAR_LENGTH(original) THEN
LEAVE loop_label;
END IF;
SET ch = SUBSTRING(original,i,1);
IF NOT ch REGEXP pattern THEN
SET temp = CONCAT(temp,ch);
ELSE
SET temp = CONCAT(temp,replacement);
END IF;
SET i=i+1;
END LOOP;
ELSE
SET temp = original;
END IF;
RETURN temp;
END;
$$
DELIMITER ;
When I put this in phpMyAdmin, my function is created without any problems.
When I click on "Add Routine" in MySQL Workbench and then paste this code, I get this error:
Syntax error: 'DELIMITER' (identifier) is not valid input at this position
I'm no expert on MySQL functions, so I don't really know what to do with this. What is the problem here?
I really want to fix this, because MySQL Workbench now shows my function as _SYNTAX_ERROR instead of regex_replace.
When using the builtin procedure editor, MySQL Workbench adds a few extra commands:
USE `test`; // <----------
DROP procedure IF EXISTS `p2`; // <----------
DELIMITER $$
USE `test`$$ // <----------
CREATE PROCEDURE test.`p2` ()
LANGUAGE SQL
DETERMINISTIC
COMMENT 'Adds "nson" to first and last names in the record.'
BEGIN
SELECT 'Hello World';
END $$
DELIMITER ; // <----------
If you apply the above slashes to your procedure, it should help :)
For reference:
Stored Procedures Using MySQL Workbench
Ok let me try this again.
query("CALL getemployee('$eml')");
$result = $sql->fetch_array();
?>
This is my stored procedure:
Delimiter //
Create procedure getemployee(in eml varchar(50))
Begin
Select * from employees where email = eml;
End//
Delimiter ;
The error i get from browser:
"Fatal error: Call to a member function fetch_array() on a non-object ...".
I use phpmyadmin version 3.2.4 and the mysql client version: 5.1.41
Your CREATE PROCEDURE statement appears to be invalid.
You need to give a name to your procedure, and to the parameter that you are passing. Therefore you may want to try something like the following example:
DELIMITER //
CREATE PROCEDURE procName (IN eml varchar(50))
BEGIN
SELECT * FROM employees WHERE email = eml;
END//
DELIMITER ;
The eml variable you use is not defined. Should not it be as following:
Create procedure getemployee (in eml varchar(50))
CREATE PROCEDURE get_customers(IN inCustomerIdList VARCHAR(100))
BEGIN
SELECT first_name, last_name, email FROM tbl_customers
WHERE customer_id IN (inCustomerIdList);
END$$