Can't figure out what's wrong with SQL syntax - mysql

I am getting 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 'if (t=null) then update employee set grade='x' where empid=id; END' at line 4
I am not able to understand where the syntax is wrong.
I am creating a procedure to get a grade from the table if it grade is present. If it is not present then it should be updated as x.
CREATE PROCEDURE spGETgrade (in id int)
BEGIN
select grade as t from employee where empid=id
if (t=null) then
update employee set grade='x' where empid=id;
END $$

I think you need ";" after the first query
select grade as t from employee where empid=id; <<---
and
IF THEN
...
END IF; <<--

Related

Syntax error trying to write SQL procedure

I have a table called "instructors" that has columns "name", "dept_name", "salary"
I'm trying to create a procedure that takes a department name (dept_name) for IN and returns the highest and lowest salaries in that department as two OUTs.
DELIMITER //
create procedure min_max(in dept_name_ varchar(20),
out max_salary numeric(12,2),
out min_salary numeric(12,2))
BEGIN
select max(salary) into max_salary,
from instructor
where instructor.dept_name = min_max.dept_name_;
select min(salary) into min_salary,
from instructor
where instructor.dept_name = min_max.dept_name_;
END//
DELIMITER ;
I am getting 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 'from instructor
where instructor.dept_name = min_max.dept_name_;
' at line 6
I've gone through the procedure a couple times, I cannot figure out what might be causing the error.

MySQL: Trying to Create a Trigger Where Another Table is Updated

I'm trying to create a trigger whereby an insertion on one table updates another. This is my SQL Query:
CREATE TRIGGER makePayment AFTER INSERT ON Payments FOR EACH ROW
BEGIN
UPDATE Invoice
SET InvoiceClientPaid = SUM(InvoiceClientPaid + NEW.PaymentAmt)
WHERE InvoiceID = NEW.PaymentInvoiceID;
END;
No matter what I do I get the following 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 '' at line 6
I don't think it's related to the SUM, because trying a basic = 1 on the SET command gives me the exact error. There is no '' at line 6 which is very confusing?
If you're entering this query directly into MySQL you will need to change the delimiter prior to the query using (e.g.) DELIMITER //, otherwise it thinks the query ends at the ; at the end of your UPDATE statement. MySQL then sees an END with nothing before it and complains about the nothing (''). So try this:
DELIMITER //
CREATE TRIGGER makePayment AFTER INSERT ON Payments FOR EACH ROW
BEGIN
UPDATE Invoice
SET InvoiceClientPaid = SUM(InvoiceClientPaid + NEW.PaymentAmt)
WHERE InvoiceID = NEW.PaymentInvoiceID;
END; //
DELIMITER ;

MySQL Syntax Error when Select Count into inout Variable

Here is the code:
CREATE PROCEDURE CountOrderByStatus(
IN orderStatus VARCHAR(25),
OUT total INT)
BEGIN
SELECT count(orderNumber)
INTO total
FROM orders
WHERE status = orderStatus;
END
When I try to execute this, i get the following error:
Error SQL (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 8
I was googling for a while, but I can't find any information about this problem. Many sources does not refer such type of code as invalid. For example, fragment above found at this site.
Any ideas, why everybody is saying, that is normal, but MySQL produces such strange error?
P.S.: MySQL Server 5.6 at Windows 8.1.
Was forget to add DELIMITER. It must look:
DELIMITER $$
CREATE PROCEDURE CountOrderByStatus(
IN orderStatus VARCHAR(25),
OUT total INT)
BEGIN
SELECT count(orderNumber)
INTO total
FROM orders
WHERE status = orderStatus;
END$$
DELIMITER ;

How to do pattern matching to a variable in mysql

So im creating a procedure that accepts an argument namee and returns all users whose name is a substring of namee
create procedure search_res(IN namee varchar(50))
begin
select * from usr where name like %namee%;
end
But im getting the following 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 '%namee%; end' at line 3.
What is the correct syntax?
Use CONCAT function
select * from usr where name like CONCAT('%',namee,'%');

Error #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

Someone tell where there is a bug?
CREATE PROCEDURE catalog_get_departments_list()
BEGIN
SELECT department_id, name FROM department ORDER BY department_id;
END$$
And this error crashes:
#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
I think you should remove dollars at the end:
CREATE PROCEDURE catalog_get_departments_list()
BEGIN
SELECT department_id, name FROM department ORDER BY department_id;
END
or set delimiter
DELIMITER $$
CREATE PROCEDURE catalog_get_departments_list()
BEGIN
SELECT department_id, name FROM department ORDER BY department_id;
END$$
One more variant for your MySQL client -
CREATE PROCEDURE catalog_get_departments_list()
SELECT department_id, name FROM department ORDER BY department_id;