SQL If Statment / select into in a stored procedure - mysql

I cannot understand why the following SQL procedure will not store on my database and is reporting an error, I've been at it for ages and am totally puzzled.
DELIMITER $$
CREATE PROCEDURE add_brand(IN inBrandName TEXT)
BEGIN
DECLARE brandexists INT DEFAULT 0;
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
IF brandexists = 1 THEN
select "exists";
ELSE
INSERT INTO tblBrand (BrandName) VALUES (inBrandName);
SELECT LAST_INSERT_ID();
END IF;
END
The error i'm getting is 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 'WHERE BrandName = inBrandName INTO brandexists; IF brandexists = 1 THEN sele' at line 6
any ideas?

You are missing a table where you select from:
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
^---here (from some_table)

Related

MySQL creating procedure getting error I cannot figure out why

I want to create a procedure for a MySql database but I get this error below and after 2h of research I still cannot figure out why.
The code is the following :
delimiter //
create procedure mitarbeiter_projekt (proj_name varchar(20), mitarb_name varchar(20))
begin
declare pruef_id int;
declare new_id int;
declare mitarb_id int;
select count(id) from t_proj where name = proj_name into pruef_id;
select id from t_ma_dt where name = mitarb_name into mitarb_id;
if pruef_id = 0 then
select max(id) + 1 from t_proj into new_id;
insert into t_proj (id, name) values (new_id, proj_name);
insert into t_ma_proj (ma_id, proj_id )values (mitarb_id, new_id);
else
select id from t_proj where name = proj_name into new_id;
insert into t_ma_proj (ma_id, proj_id) values (mitarb_id, new_id);
end //
"ERROR 1064 (42000): 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 15"
all of the above tables I used exist in my database.
Thankful for any help!

Cannot run mysql syntax on select

I tried running this in phpmyadmin...
It turned out something is not right.
I cannot figure out what is wrong here.
DELIMITER ;
create definer=proiect_wd_user#localhost FUNCTION
f_suma(id_s int, price_f double,product_code_n char(255),quantity_b int)
returns double
BEGIN
select price_f into #price_f
from orders_details WHERE (id=id_s)
select quantity_b into #quantity_b
from orders_details WHERE (id=id_s)
set #suma_f=(#price_f*#quantity_b);
RETURN #suma_f;
end ;
Error:
MySQL said: Documentation
#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 'select quantity_b into #quantity_b
from orders_details WHERE (id=id_s)
set #su' at line 7
There are various problems here with your delimiters. See Working with Stored Procedures in the MySQL documentation.
DROP FUNCTION IF EXISTS f_suma;
DELIMITER //
CREATE DEFINER=proiect_wd_user#localhost
FUNCTION f_suma(id_s int,price_f double,product_code_n char(255),quantity_b int)
RETURNS double
BEGIN
SELECT price_f INTO #price_f FROM orders_details WHERE (id=id_s);
SELECT quantity_b INTO #quantity_b FROM orders_details WHERE (id=id_s);
SET #suma_f=(#price_f*#quantity_b);
RETURN #suma_f;
END //
DELIMITER ;

MySQL procedure results in a syntax error

This is my MySQL procedure. I got an error while executing this.
DELIMITER //
CREATE PROCEDURE GET_ORDER_HISTORY_LIST(IN distid int)
BEGIN
SELECT
ort.order_id,
ort.transaction_id,
ort.user_id,
ort.transaction_date,
ort.insert_by,
ort.organization_id,
odt.course_id,
count(odt.quantity),
ct.course_name,
ct.course_code
FROM cdp_order_master as ort
JOIN cdp_order_detail as odt ON odt.order_id = ort.order_id
JOIN cdp_course as ct ON ct.course_id = odt.course_id
WHERE ort.user_id = distid
GROUP BY ort.order_id
END //
DELIMITER ;
Error is
#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 'END' at line 19
DELIMITER //
CREATE PROCEDURE GET_ORDER_HISTORY_LIST(IN distid int)
BEGIN
SELECT
ort.order_id,
ort.transaction_id,
ort.user_id,
ort.transaction_date,
ort.insert_by,
ort.organization_id,
odt.course_id,
count(odt.quantity),
ct.course_name,
ct.course_code
FROM cdp_order_master as ort
JOIN cdp_order_detail as odt ON odt.order_id = ort.order_id
JOIN cdp_course as ct ON ct.course_id = odt.course_id
WHERE ort.user_id = distid
GROUP BY ort.order_id; //I had made change at this line.
END //
DELIMITER ;
You can try above query.
Here is a SQL Fiddle.

mysql stored procedure declare error

I don't really get the error ...
i put the declare statement right after begin. i usually do a lot of MS SQL so i am not that practiced in mysql syntax. please help me out
DELIMITER //
CREATE DEFINER=`root`#`localhost` PROCEDURE `AddEntry2`(IN `inDate` DATE, IN `inUser` INT, IN `inComment` TEXT, IN `inStart` INT, IN `inEnd` INT)
BEGIN
DECLARE commID UNSIGNED;
INSERT IGNORE INTO dimcomment (comment) values (inComment);
SELECT commID := MAX(id)
FROM dimcomment
WHERE comment = inComment;
INSERT INTO factentry (FKDate, FKUser, FKComment,FKStart,FKEnd,FKAbsence)
VALUES (
inDate,
inUser,
commID,
inStart,
inEnd,
1
);
END //
Error I get:
#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 'UNSIGNED;
INSERT IGNORE INTO dimcomment (comment) values (inComment);
SELECT c' at line 3
well i changed UNSIGNED to INT and Error Message changed:
#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 ':= MAX(id)
FROM dimcomment
' at line 5
Seems like i cannot set via SELECT VAR := VALUE ?

issue with create table inside if/else block

I'm trying to create a table in a MySQL stored procedure depending upon the values of some of the passed parameters. My code is as follows:
DELIMITER //
CREATE PROCEDURE testifelse(IN CurrentGPA varchar(40), IN CurrentGPAValue varchar(40))
BEGIN
IF CurrentGPAValue IS NULL THEN CREATE TABLE tableCurrentGPA(SELECT DISTINCT u.user_id AS uid FROM users u);
ELSE CREATE TABLE tableCurrentGPA(SELECT DISTINCT udata.user_id AS uid FROM userdata udata WHERE udata.userDataTypeName = CurrentGPA AND udata.userDataText = CurrentGPAValue);
END
I'm getting an error stating: #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 5.
I'm not sure what I've done wrong here. The syntax looks fine to me and the queries seem well-formulated.
You're missing the END IF statement.
CREATE PROCEDURE testifelse(IN CurrentGPA varchar(40), IN CurrentGPAValue varchar(40))
BEGIN
IF CurrentGPAValue IS NULL THEN CREATE TABLE tableCurrentGPA(SELECT DISTINCT u.user_id AS uid FROM users u);
ELSE CREATE TABLE tableCurrentGPA(SELECT DISTINCT udata.user_id AS uid FROM userdata udata WHERE udata.userDataTypeName = CurrentGPA AND udata.userDataText = CurrentGPAValue);
END IF;
END