MySQL stored Procedure error with IF...THEN...END IF; statements - mysql

I have a MySQL stored procedure that throws an error.
DELIMITER $$
DROP PROCEDURE IF EXISTS `test_schema`.`TEST_SPROC` $$
CREATE PROCEDURE `test_schema`.`TEST_SPROC` (IN var0 INT, var1 INT)
BEGIN
DECLARE var0 INT;
DECLARE var1 INT;
SELECT COUNT(*) INTO var0 FROM INFORMATION_SCHEMA.`TABLES`
WHERE `TABLE_SCHEMA`='test_schema' AND `TABLE_NAME`='original_table';
SELECT COUNT(*) INTO var1 FROM INFORMATION_SCHEMA.`COLUMNS`
WHERE `TABLE_SCHEMA`='test_schema' AND `TABLE_NAME`='new_table'
AND `COLUMN_NAME`='id';
IF var0=1 THEN
RENAME TABLE test_schema.original_table TO test_schema.new_table;
END IF;
IF var1=1 THEN
ALTER TABLE test_schema.new_table CHANGE id AccountID VARCHAR;
END IF; #error is thrown here.
END $$
DELIMITER ;
Error:
Script line: 4 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 IF;
END' at line 15
What is wrong with my if statement?

Change this line:
ALTER TABLE test_schema.new_table CHANGE id AccountID VARCHAR;
to this:
ALTER TABLE test_schema.new_table CHANGE id AccountID VARCHAR(100);
Of course you should specify a length for the VARCHAR column that is appropriate. I've just used VARCHAR(100) as an example.

You have to specify a length for the alter table statement. See the comment below:
IF varTable=1 THEN
RENAME TABLE test_schema.original_table TO test_schema.new_table;
SELECT COUNT(*) INTO varColumn FROM INFORMATION_SCHEMA.`COLUMNS`
WHERE `TABLE_SCHEMA`='test_schema' AND `TABLE_NAME`='new_table'
AND `COLUMN_NAME`='id';
IF varColumn=1 THEN
#The following statement must be "VARCHAR(255)" not just "VARCHAR"
ALTER TABLE test_schema.new_table CHANGE id AccountID VARCHAR(255);
ELSE
#statements.
END IF;
END IF;

Related

Nested Cursor Declare Issue Mysql

I'm trying to make a Nested Cursor in Mysql by following this instruction.
Then i got this issue:
#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 'DECLARE activityids CURSOR FOR SELECT activity_id FROM #_activity;
END BLOCK2;' at line 22
I've 2 table 'account' and 'n_activity' (n = account_id in table 'account')
Ex: i've table 'account' and '20_activity'.
So i want to loop the 'account_id' and get the 'activity_id' from that loop.
Here is my code:
DROP PROCEDURE if exists update_schema_activity_startdate_and_duedate;
DELIMITER $$
CREATE PROCEDURE update_schema_activity_startdate_and_duedate()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE accountid INT;
--
-- GET ALL ACCOUNT ID
--
DECLARE accountids CURSOR FOR SELECT account_id FROM account;
--
-- LOOP
--
OPEN accountids;
read_loop: LOOP
FETCH accountids INTO accountid;
BLOCK2: BEGIN
SET #_activity = CONCAT(accountid,'_activity');
DECLARE activityids CURSOR FOR SELECT activity_id FROM #_activity;
END BLOCK2;
END LOOP;
CLOSE accountids;
END$$
DELIMITER ;
CALL update_schema_activity_startdate_and_duedate();
Please help, thanks.

Procedure not working using "IF"

I have the fallowing procedure:
DELIMITER $$
CREATE OR REPLACE PROCEDURE add_comments
AS
BEGIN
DECLARE var INT;
SELECT grade INTO var FROM table1;
IF (var <= 7) THEN
UPDATE table2 set comment = not_good;
ELSE
UPDATE table2 set comment = good;
END IF;
END$$
comment, not_good and good are all columns from table2
but it gives me the 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 'AS
BEGIN
IF (var <= 7)
BEGIN
UPDATE table2 set comment ' at line 3
Can't find out the problem, can someone help me with this?
Thanks!
Replace
DELIMITER $$
CREATE OR REPLACE PROCEDURE add_comments
AS
BEGIN
With
DELIMITER $$
CREATE OR REPLACE PROCEDURE add_comments()
BEGIN
PS: add brackets and remove "AS"
Remove that AS operator from your procedure code body. Your procedure should look like
DELIMITER $$
CREATE OR REPLACE PROCEDURE add_comments
BEGIN
DECLARE var INT;
SELECT grade INTO var FROM table1;
IF (var <= 7) THEN
UPDATE table2 set comment = not_good;
ELSE
UPDATE table2 set comment = good;
END IF;
END$$
Refer MySQL Documentation for more information

how to works on insert,update data from a table using store Procedure

if i use one table in two different Stored procedure name, (one for insert , one for update command) it showing syntax error.
first i created studentrc SP:
delimiter //
create procedure studentrc(in student_name varchar(20),in Reg_no int(6),in mark1 int(3), in mark2 int(3),in total int(10))
begin
insert into studentrecords values(student_name,Reg_no,mark1,mark2,total);
end; //
no errors
next i create studentrcs SP:
delimiter //
create procedure studentrcs(inout Reg_no int(6))
begin
UPDATE studentrecords
set student_name=?,mark1=?,mark2=?,total=?
where Reg_no=?;
end;//
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 'UPDATE studentrecords
set student_name=?,mark1=?,mark2=?,total=?
where Reg_no=' at line 3
how can rectify this error...
I'll be the first to admit I'm not a SQL guru by any means, but shouldn't you be taking in more variables and using them in place of the question marks?
You need to put the news values in the args list
create procedure studentrcs(sname varchar(100),m1 varchar(100),m2 varchar(100),total int,inout Reg_noarg int(6))
begin
UPDATE studentrecords
set student_name=sname ,mark1=m1,mark2=m2,total=totalarg
where Reg_no=Reg_noarg ;
for "INSERT"
DELIMITER $$
DROP PROCEDURE IF EXISTS `mydatabase`.`myprocedurename` $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `myprocedurename`(IN field1 VARCHAR(50),
IN field2 INT(10),IN field3 INT(10), IN field4 VARCHAR(10))
BEGIN
INSERT INTO mytablename (FIELD_1,FIELD_2,FIELD_3,FIELD_4) VALUES
(field1,field2,field3,field4);
END $$
DELIMITER ;
size which i have given in IN parameter should be equal to field size in table
for "UPDATE"
DELIMITER $$
DROP PROCEDURE IF EXISTS `mydatabase`.`myprocedurename` $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `myprocedurename`(IN field1 VARCHAR(50),
IN field2 INT(10) )
BEGIN
UPDATE mytablename SET FIELD_1=filed1 WHERE FIELD_2=field2;
END $$
size which i have given in IN parameter should be equal to field size in table
hope this may help you.

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

declare and assign value my sql stored procedure(5.0.45)

DELIMITER $$
DROP PROCEDURE IF EXISTS quotations.sp_addservices $$
CREATE PROCEDURE quotations.sp_addservices
(In categoryname varchar(25),in servicename varchar(250),in hours float,in cost float,in basis nvarchar (100))
BEGIN
insert into categorydetails (Category_Name) values (categoryname);
if(categoryname!=null)
then
DECLARE category_id int;
set category_id= select max(Category_Id) from categorydetails ;
insert into servicesdetails (Service_Name,Category_Id,Hours,Cost,Basis) values(servicename,category_id,hours,cost,basis);
end if;
END $$
DELIMITER ;
This is my stored procedure .I have to retrive the value of categoryid that is posted into the database which is auto increased.Here i cant declare the variable and assign value to variable.Am getting error like
Script line: 4 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 'DECLARE category_id int;
set category_id= select max(Category_Id) from categor' at line 9
Can any one help me
Thanks in advance.
Try
SELECT MAX(c.category_id) INTO category_id FROM categorydetails c;