I'm trying to create a procedure which (1)adds new attribute called 'Hours' to table called 'Project' (if it does not have the 'Hour' attribute) and (2)initialize the value of 'Hour' to 0.0. Below is the code I tried (using myPhpAdmin) but it gives me SQL syntax error. What did I do wrong?
The error message says "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 Project SET Hours = 0.0 where Project.Pnumber = p end if; end'
"
delimiter &
create procedure init(in p int(11))
begin
declare attNum int;
SELECT COUNT(*) INTO attNum FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'MyDB'
AND table_name = 'Project';
if attNum < 5 then ALTER TABLE Project ADD Hours float;
end if;
SELECT * FROM Project
where Project.Pnumber = p
UPDATE Project SET Hours = 0.0
where Project.Pnumber = p
end
Semicolon is missing in the end of Select and Update query in Stored procedure creation query.
Corrected Query:
delimiter &
create procedure init(in p int(11))
begin
declare attNum int;
SELECT COUNT(*) INTO attNum FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = 'MyDB'
AND table_name = 'Project';
if attNum < 5 then ALTER TABLE Project ADD Hours float;
end if;
SELECT * FROM Project
where Project.Pnumber = p;
UPDATE Project SET Hours = 0.0
where Project.Pnumber = p;
end