MySQL creating procedure getting error I cannot figure out why - mysql

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!

Related

MySQL Stored Procedure Case Statement Syntax Error - Contd 2

One Syntax Error in the following code.
The response of the mysql was
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 ''300ml','500ml','1lit','2lit')
VALUES(m_cust_id,IN_book_id,(SELECT Rate.Can*Book' at line 24 SQL
Statement
The following is the procedure.
CREATE PROCEDURE `calculate_amount` (in IN_book_id INT, in IN_qty INT )
BEGIN
-- declare
DECLARE m_prdct VARCHAR(10);
DECLARE m_cust_id INT(5);
-- select into
SELECT
Product
FROM
Bookings
WHERE
Book_id = IN_book_id INTO m_prdct;
SELECT
Cust_id
FROM
Bookings
WHERE
Book_id = IN_book_id INTO m_cust_id;
-- conditionals & action
IF (m_prdct = '20ltr') THEN
INSERT INTO Amount (Cust_id,Book_id,Can,'300ml','500ml','1lit','2lit') VALUES(m_cust_id,IN_book_id,(SELECT Rate.Can*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id),0,0,0,0);
ELSEIF (m_prdct = '300ml') THEN
INSERT INTO Amount(Cust_id,Book_id,Can,'300ml','500ml','1lit','2lit') VALUES(m_cust_id,IN_book_id,0,(SELECT Rate.300ml*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id),0,0,0);
ELSEIF (m_prdct = '500ml') THEN
INSERT INTO Amount(Cust_id,Book_id,Can,'300ml','500ml','1lit','2lit') VALUES(m_cust_id,IN_book_id,0,0,(SELECT Rate.500ml*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id),0,0);
ELSEIF (m_prdct = '1ltr') THEN
INSERT INTO Amount(Cust_id,Book_id,Can,'300ml','500ml','1lit','2lit') VALUES(m_cust_id,IN_book_id,0,0,0,(SELECT Rate.1lit*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id),0);
ELSE
INSERT INTO Amount(Cust_id,Book_id,Can,'300ml','500ml','1lit','2lit') VALUES(m_cust_id,IN_book_id,0,0,0,0,(SELECT Rate.2lit*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id));
-- end
END IF;
END
The script applied successfully even if it showed error. The Back Tick to column names starting with digits solved the issue. Upon Refreshing, it got changed from _SYNTAX_ERROR to its actual name.

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 stored procedure and COUNT IF statement

So I need to make a stored procedure called AddComment, that will add a comment to my Comments table , then add an entry into the Commenters table if that commenters name does not exist.
DELIMITER //
CREATE PROCEDURE AddComment(Name VARCHAR(60), Title VARCHAR(60), Comments VARCHAR(60))
BEGIN
INSERT INTO Comments(Name, Title, Comments)
VALUES (Name, Title, Comments);
DECLARE name_count INT;
SELECT COUNT(Name) INTO name_count
FROM Commenters
WHERE Name = Name;
IF name_count = 0
THEN INSERT INTO Commenters(Name)
VAlUES(Name);
ELSEIF name_count = 1
THEN INSERT IGNORE Commenters(Name)
VALUES(Name);
END IF;
END;
//
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version fo
r the right syntax to use near 'DECLARE name_count INT;
SELECT COUNT(Name) INTO name_count
FROM Commenters
^ Is the error I'm getting. I keep tweaking my code trying to figure it out, but nothing is working.
You need a cursor to use syntax like that, but you can use SET:
SET name_count = (SELECT COUNT(*) ...);
Or better yet, since you don't actually use the value, eliminate the variable entirely:
IF NOT EXISTS (SELECT * FROM ...) THEN

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)

Mysql stored procedure syntax

I am trying to write simple mysql stored procedure and it seems that I can't get it right, so far I have
delimiter //
create procedure addRecord(_login varchar(15),_artist varchar(50),_record varchar(50))
begin
declare dbArtist varchar(50);
delcare dbRecord varchar(50);
set dbArtist = (select artistname from artists where lower(artistname) = lower(_artist));
set dbRecord=(select recordname from records where lower(recordname)=lower(_record));
if not exists (select * from Artists where lower(artistname)=lower(_artist)) then
begin
INSERT INTO `Artists`(`ArtistName`) VALUES (_artist);
set dbArtist=_artist;
end
if not exists (select * from Records as R inner join Artists as A on R.ArtistId=A.ArtistId where lower(R.RecordName)=lower(_record) and A.ArtistName=dbArtist) then
begin
INSERT INTO `Records`(`ArtistId`, `RecordName`) VALUES ((select artistid from artists where artistname=dbArtist),_record);
set dbRecord=_record;
end
end
but I get syntax error in line 4:
#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 'dbRecord varchar(50);
set dbArtist = (select artistname from artists where lowe' at line 4
this message error was returned to me by phpMyAdmin, can anyone tell me why do I get an error?
edit: modified version, still not good
delimiter //
create procedure addRecord(_login varchar(15),_artist varchar(50),_record varchar(50))
begin
declare dbArtist varchar(50);
declare dbRecord varchar(50);
set dbArtist = (select artistname from artists where lower(artistname) = lower(_artist));
set dbRecord=(select recordname from records where lower(recordname)=lower(_record));
if not exists (select * from Artists where lower(artistname)=lower(_artist)) then
begin
INSERT INTO `Artists`(`ArtistName`) VALUES (_artist);
set dbArtist=_artist;
end
if not exists
(select * from Records as R inner join Artists as A on R.ArtistId = A.ArtistId where lower(R.RecordName)=lower(_record) and A.ArtistName=dbArtist)
then
begin
INSERT INTO `Records`(`ArtistId`, `RecordName`) VALUES ( (select artistid from artists where artistname=dbArtist) ,_record);
set dbRecord=_record;
end
end
now error in line 14 and message:
#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 'if not exists (select * from Records as R inner join Artists as A on R.ArtistId' at line 14
The problem is that you misspelled DECLARE:
delcare dbRecord varchar(50);
UPDATE: For your next error, the problem is your illegal use of NOT EXISTS.
Within a stored procedure the proper approach is to count the existing rows, and then conditionally insert a value if the count is 0.
Something like this:
SELECT COUNT(*)
INTO #v_row_count
FROM Artists
WHERE LOWER(artistname)=LOWER(_artist);
IF (#v_row_count = 0)
THEN
INSERT INTO `Artists`(`ArtistName`) VALUES (_artist);
set dbArtist=_artist;
END IF;
P.S. To avoid poor performance on on your select query, you should consider using a collation that is not case-sensitive so you don't need to apply the LOWER() function to the artistname column.