Search code examples
mysqlstored-proceduressyntax-errordeclare

How to create stored procedure in mysql workbench?


I know Microsoft sql server and I think this codes doesn't contain an error. But Mysql thow an exeption which I Couldn't undestant. Is anybody can explain it for me and please,give me mysql function,view,stored procedure examples website name

CREATE PROCEDURE spInsertEmployee
(
 Employee_Name nvarchar(20),
 Employee_Surname nvarchar(20),
 Department_Name nvarchar(20)
)
BEGIN
declare Department_Id int; /*error*/
select Id into Department_Id from tblDepartment where      tblDepartment.Department_Name=Department_Name;
insert into tblVeri(Employee_Name,Employee_Surname,Department_Id)     values(Employee_Name,Employee_Surname,Department_Id);
END /*error*/

Solution

  • You must define IN for the parameters.

    DELIMITER $$
    
    CREATE PROCEDURE spInsertEmployee 
    (
    IN Employee_Name nvarchar(20), 
    IN Employee_Surname nvarchar(20), 
    IN Department_Name nvarchar(20)
    )
    BEGIN
    declare Department_Id INT;
    
    select Id into Department_Id from tblDepartment WHERE tblDepartment.Department_Name=Department_Name;
    insert into tblVeri(Employee_Name,Employee_Surname,Department_Id)     VALUES(Employee_Name,Employee_Surname,Department_Id);
    
    END;