Following is my code:
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `employee_with_asset`(IN name VARCHAR(250))
BEGIN
SELECT a.Asset_code,a.name as name1,a.type,a.description,`purchase date`,
`amc availability`,`amc renewal`,`employee Id`,b.Name FROM `asset_details` a,
employee b WHERE b.Name LIKE '%' + @name + '%' and a.`assigned to`=b.`employee Id`;
END
It is showing error near LIKE. How to solve it.
The value of the parameter is used along with LIKE operator (statement) in a SELECT statement. In the above Stored Procedure, the LIKE operator (statement) works as CONTAINS where it looks for the match throughout the string value. You can also use it as STARTS WITH and ENDS WITH options as shown below.
The MySQL LIKE Operator The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.
In the case of MySQL, procedures are written in MySQL and stored in the MySQL database/server. A MySQL procedure has a name, a parameter list, and SQL statement(s). There are four different types of MySQL procedures: 1.
Create a simple stored procedure. DELIMITER ; To create the MySQL Stored Procedure, open the MySQL workbench Connect to the MySQL Database copy-paste the code in the query editor window click on Execute. You can view the procedure under stored procedures.
the concatenation in mysql is done using CONCAT()
LIKE CONCAT('%', @name , '%')
FULL STATEMENT
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `employee_with_asset`
(
IN _name VARCHAR(250)
)
BEGIN
SELECT a.Asset_code,
a.name as name1,
a.type,
a.description,
`purchase date`,
`amc availability`,
`amc renewal`,
`employee Id`,
b.Name
FROM `asset_details` a
INNER JOIN employee b
ON a.`assigned to` = b.`employee Id`
WHERE b.Name LIKE CONCAT('%', _name , '%');
END $$
DELIMITER ;
The answer of John Woo is right but if you don't use utf8_general_ci collection, you must cast collection after WHERE clause (after each condition if you have multiple conditions) as the same as collection of the column you compare to like this
... WHERE name LIKE CONCAT('%', @name , '%') COLLATE utf8_unicode_ci
or multiple conditions
... WHERE name LIKE CONCAT('%', @name , '%') COLLATE utf8_unicode_ci
OR job LIKE CONCAT('%', @name , '%') COLLATE utf8_unicode_ci ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With