What is wrong with my mysql stored procedure? - mysql

I have a stored procedure that checks whether or not a new entry is already existing in the table. if it exists, the insert will not happen. when I run it, there is an error
DROP PROCEDURE IF EXISTS AddPriority2;
DELIMITER $$
CREATE PROCEDURE AddPriority2
(
IN strName VARCHAR(100),
OUT itExists INT
)
BEGIN
DECLARE
SELECT COUNT(Id) INTO itExists
FROM priorities
WHERE Name = strName AND StatId = 1;
IF(itExists = 0) THEN
INSERT INTO priorities
(
NAME,
StatId
)
VALUES
(
strName,
1
);
END IF;
END
here is the error
Query: CREATE PROCEDURE AddPriority2 ( IN strName VARCHAR(100), OUT itExists INT ) BEGIN DECLARE SELECT COUNT(Id) INTO itExists FROM pr...
Error Code: 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 COUNT(Id) INTO itExists
FROM priorities
WHERE Name = strName AND StatId =' at line 8

1) You cannot declare a select statement - a declare has to be for a variable..(and I would not use an output parameter for that) 2) or you can use exists instead
if not exists (select 1 from priorities WHERE Name = strName AND StatId = 1) then
insert...
end if;

Related

Create a function which return table value in mysql

Hi all I have problem in creating a function in Mysql.
1.I have to create a function which will get a input string which consists of date's in comma seperated value and i want to convert it into multiple date records through a function
please find the below sample input
set #value = '01-02-2017,01-03-2017,04-03-2017,07-03-2017,13-03-2017,14-03-2017,17-03-2017,18-03-2017,31-03-2017';
query that i have tried is shown below
delimiter $$
create function fn_split(value varchar(255))
returns int
begin
declare num int;
declare val int;
set num := 1;
set val := (select char_length(value)-char_length(replace(value,',','')));
create temporary table if not exists my_tab(date1 datetime);
delete from my_tab;
while num>val
insert into mytab (date1)
select SUBSTRING_INDEX(value,',',num);
set num = num + 1;
end while;
select count(date1) into count1 from my_tab;
return (count1);
end $
delimiter;
error that i am getting
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 'insert into mytab (date1) select SUBSTRING_INDEX(value,',',num); set num = num +' at line 11
Thanks in advance.

Synatx error near 'IF EXISTS'

I want to solve an error.
While i am exicuting this query i met with an error.
IF EXISTS (SELECT * FROM category_info WHERE category_name = 'Electronics')
BEGIN
select * from category_info;
END
ELSE
BEGIN
insert into category_info(category_name) values('Electronics');
END
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 EXISTS (SELECT * FROM category_info WHERE category_name =
'Electronics') BEG' at line 1
I need help,Thanks in advance
It can be solved by using procedure.
delimiter $$
CREATE PROCEDURE Check_exists(IN categoryName varchar(20))
BEGIN
IF EXISTS(SELECT * FROM category_info WHERE category_name = categoryName)
THEN
SELECT 'Already Exists' as status;
ELSE
INSERT INTO category_info(category_name) VALUES(categoryName);
END IF;
END $$
and this procedure can be called by
call Check_Exists('Electronics');
It will display message "Already exists " if it is exists or else it will insert a row.

Error in Sql Query - Can't find the solution

I'm having big troubles to find the error in my sql query.
CREATE FUNCTION freeSeats(bookingID INT)
RETURNS VARCHAR(30)
BEGIN
DECLARE numberBooked INT ;
DECLARE status VARCHAR(30);
SELECT count(*) FROM passenger WHERE Booking IN(SELECT Id FROM booking WHERE
Flight = (SELECT Flight FROM booking WHERE Id = bookingID)) INTO numberBooked;
IF (numberBooked > 59) THEN SET status =”No free seats”;
ELSE SET status =”OK”;
END IF;
RETURN status;
END;
I get this error message:
ERROR 1064 (42000): 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 'free seats”; ELSE SET status =”OK”; END IF;
RETURN status; END' at line 10
I would appreciate some help.
Thanks in advance
don't for get to change the DELIMITER
use single quotes
example,
DELIMITER $$
CREATE FUNCTION freeSeats(bookingID INT)
RETURNS VARCHAR(30)
BEGIN
DECLARE numberBooked INT ;
DECLARE status VARCHAR(30);
SET numberBooked =
(
SELECT count(*)
FROM passenger
WHERE Booking IN
( SELECT Id
FROM booking
WHERE Flight = (SELECT Flight FROM booking WHERE Id = bookingID)
)
);
IF (numberBooked > 59) THEN
SET status = 'No free seats';
ELSE
SET status = 'OK';
END IF;
RETURN status;
END $$
DELIMITER ;

Unable to CREATE FUNCTION in MYSQL ERROR 1064 (42000)

Hi I'm using MySQL to create a function:
CREATE FUNCTION INSERTGROUP(name VARCHAR(50))
RETURNS INT
NOT DETERMINISTIC
BEGIN
DECLARE 'idGroup' INT;
IF (NOT EXISTS (SELECT groupID FROM groupsTable WHERE groupName = 'name'))
THEN INSERT INTO groupsTable (groupName) VALUES ('name');
SELECT groupID INTO 'idGroup' FROM groupsTable WHERE groupName='name';
RETURN 'idGroup';
END//
But I get this error when I try to create it:
ERROR 1064 (42000): 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
''idGroup' INT;
IF (NOT EXISTS (SELECT groupID FROM serverGroupsTable WHERE gro' at line 5
I've been through forums and other questions similar to this one but I can not make it create the function.
What I'm doing wrong? Is the syntax correct? Do I need to add something else?
Ok this is what I've try:
CREATE FUNCTION INSERTGROUP(name VARCHAR(255))
RETURNS INT
NOT DETERMINISTIC
BEGIN
DECLARE idGroup INT;
IF (NOT EXISTS (SELECT groupID FROM groupsTable WHERE groupName = name))
THEN INSERT INTO groupsTable (groupName) VALUES (name);
SELECT groupID INTO idGroup FROM groupsTable WHERE groupName=name;
RETURN idGroup;
END//
and
CREATE FUNCTION INSERTGROUP(name VARCHAR(255))
RETURNS INT
NOT DETERMINISTIC
BEGIN
DECLARE idGroup INT;
IF (NOT EXISTS (SELECT groupID FROM groupsTable WHERE groupName = 'name'))
THEN INSERT INTO groupsTable (groupName) VALUES ('name');
SELECT groupID INTO idGroup FROM groupsTable WHERE groupName='name';
RETURN idGroup;
END//
and for the last two I got this error:
ERROR 1064 (42000): 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 13
Similar to the one above
Also I do DELIMITER // to change it from ;
remove the quotes from the declaration
DECLARE idGroup INT;
and you forgot the delimiter change at the start of the function
delimiter //
CREATE FUNCTION INSERTGROUP(name VARCHAR(255))
and you forgot to end your if statement
end if;
whereever you want it to end

Insert,upadate in single Procedure in mysql

im creating stored procedure in my sql but getting error,error shows as below,help me Error Descrption:You hve an error in your sql syntax: ckeck the manual that corresponds to your MYSQL
server version fro the right syntax to use near 'END' at line 13
DELIMITER $$
DROP PROCEDURE IF EXISTS myhealthcamp.area $$
CREATE PROCEDURE myhealthcamp.area
(
IN id INT,
IN ar VARCHAR(45)
)
BEGIN
if exists (select area_id from area where area_id = id)
Then
update area set areaname = ar where area_id=id;
else
insert into area(area_id, areaname) values(id,ar);
END IF
END $$
DELIMITER ;
You're missing a ; in this line:
END IF
It should be:
END IF;
IF is a statement and all statements must end in semi-colons. This one's no different. See the MySQL documentation for IF.
DROP PROCEDURE IF EXISTS myhealthcamp.area $$
CREATE PROCEDURE myhealthcamp.area
(
IN id INT,
IN ar VARCHAR(45)
)
BEGIN
if exists (select area_id from area where area_id = id)
Then
update area set areaname = ar where area_id=id;
else
insert into area(area_id, areaname) values(id,ar);
END IF;
semicolon missing
Thanks
Sanil