MySQL procedure results in a syntax error - mysql

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.

Related

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 ;

Running the following query from my PhpMyadmin SQL tab

I'm running the following query from my PhpMyadmin SQL tab:
CREATE TRIGGER trg_bansach ON bill AFTER INSERT AS
BEGIN
UPDATE addbook
SET Quality = Quality - (
SELECT QualitySale
FROM inserted
WHERE book_id = addbook.book_id
)
FROM addbook
JOIN inserted ON addbook.book_id = inserted.book_id
END
But everytime I'm getting this error msg:
#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 'FROM addbook
WHERE addbook.book_id = inserted.book_id
END' at line 10
Use delimiter
DELIMITER //
CREATE TRIGGER trg_bansach ON bill AFTER INSERT AS
BEGIN
UPDATE addbook
SET Quality = Quality - (
SELECT QualitySale
FROM inserted
WHERE book_id = addbook.book_id
)
FROM addbook
JOIN inserted ON addbook.book_id = inserted.book_id;
END
//
DELIMITER ;

Unidentifiable error in SQL syntax, thrown when trying to store a procedure

I am getting an error when I try to enter the following procedure using the phpMyAdmin SQL box. I'm pretty sure the syntax is correct though!
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
')
BEGIN
UPDATE decks
JOIN amount ON amount.DeckName = de' at line 1
SQL SYNTAX:
delimiter $$
CREATE PROCEDURE DeleteCard(IN aCard varChar)
BEGIN
UPDATE decks
JOIN amount ON amount.DeckName = decks.DeckName
SET decks.DeckTotal = decks.DeckTotal - amount.Amount
WHERE amount.CardName = aCard ;
UPDATE types t1
JOIN cards ON cards.TypeName = t1.TypeName
JOIN Amount ON amount.CardName = cards.CardName
SET t1.TypeTotal = t1.TypeTotal - amount.Amount
WHERE amount.CardName = aCard ;
DELETE
FROM amount
WHERE cardName = aCard;
DELETE
FROM cards
WHERE cardName = aCard;
END
$$
DELIMITER ;

SQL If Statment / select into in a stored procedure

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)

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