How to create stored procedure in mysql workbench? - mysql

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*/

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;

Related

stored procudure variaibles in mysql

I am declaring variables inside the procedure and setting those values to result from another query.when executed it is giving null.
version:8.0.16
call putrequest('x',"jiraUPM","ASE-12345","inprogress","testcybsjira.com");
requestId and reqid are not null.but it taking null value .
create procedure `putrequest`(in `employeeId` varchar(15),in `reqtype`
varchar(15),in `ticketId` varchar(15),in `status` varchar(15),in `details`
varchar(100))
begin
declare `rid` int;
declare `reqType` int;
select `requestId` into `reqType` from `requesttype` where `request`=`reqtype`;
select `reqId` into `rid` from `employee` where `empId`=`employeeId`;
insert into `requests` values(rid,reqType,`ticketId`,NOW(),NOW(),`status`,`details`);
end
Error executing SQL statement. Column 'reqId' cannot be null - Connection: Connection 1: 93ms
If either of the SELECT queries doens't find a matching row, the corresponding variable will be NULL, and you'll get an error when you try to insert it. You need to check for that before doing the INSERT.
But there's no need for separate SELECT queries and variables, use INSERT INTO ... SELECT ...
INSERT INTO requests
SELECT e.reqId, r.requestId, ticketId, NOW(), NOW(), status, details
FROM requesttype AS r
CROSS JOIN employee AS e
WHERE r.request = reqtype
AND e.empId = employeeId
If reqtype or employeeId can't be found, the join won't return any rows, so nothing will be inserted.
It creates a problem because of the same variable name "reqtype" (declare as a procedure parameter) and "reqType" (Declare as procedure variable)
It considers the null value for reqtype and not return any record.
MySQL is case insensitive . Be careful while given variable name.
DELIMITER $$
create procedure `putrequest`(in `employeeId` varchar(50),in `rtype`
varchar(50),in `ticketId` varchar(15),in `status` varchar(15),in `details`
varchar(100))
begin
declare `rid` int;
declare `reqType` int;
select `reqId` into `rid` from `employee` where `empId`=`employeeId`;
select `requestId` into `reqType` from requesttype where `request`=`rtype`;
insert into `requests` values(rid,reqType,`ticketId`,NOW(),NOW(),`status`,`details`);
end$$
DELIMITER ;
DEMO

#1064 - You have an error in your SQL syntax

I already have a table employee with columns name(String),id(int),age(int).
I can't figure out where the syntax is wrong?
CREATE PROCEDURE recins (
name1 IN employee.name%type ,
id1 IN employee.id%type ,
age1 IN employee.age%type
) AS
BEGIN
INSERT INTO employee VALUES(name1,id1,age1);
END;
create table employee2
(
name varchar(100) not null,
id int not null,
age int not null
);
DELIMITER $$
CREATE PROCEDURE recins (
IN name1 varchar(100),
IN id1 int,
IN age1 int
)
BEGIN
INSERT INTO employee2 (name,id,age) VALUES(name1,id1,age1);
END $$
DELIMITER ;
-- test:
call recins('a',1,2);
delimiter is a special wrapper for stored procs, events, functions. Delimiter ; at the end of the stored proc sets it back to the normal/default delimiter of a semi-colon.
The above was tested.

mysql Insert Statements in Stored Procedure not Inserting

I created a stored procedure which should perform the following:
Insert into Agencies Table, creating an id in the PK index row
Save the id in a variable
Insert the id and other data into Users table
The syntax below does not trigger any errors:
DELIMITER $$
CREATE PROCEDURE insertNewAgencyAndAdmin (IN aName varchar (100), IN numTrav int, IN polType int, IN uEmail varchar(255), IN uFName varchar(40), IN uLName varchar(40), IN uTitle varchar(100))
BEGIN
INSERT INTO btsAgency.Agencies (agencyName, numTrav, polType) VALUES (#aName, #numTrav, #polType);
SET #agencyID = (SELECT agencyID from btsAgency.Agencies where agencyID = LAST_INSERT_ID());
INSERT INTO btsUsers.Users (userAgencyID, userEmail, userFirstName, userLastName, userTitle ) VALUES
(#agencyID, #uEmail, #uFName, #uLName, #uTitle);
END
However, the Stored Proc doesn't insert my parameters into the tables when executed. So, after searching I try to create the SP like this (including $$ after "END" and resetting the delimiter to a semi-colon ):
DELIMITER $$
CREATE PROCEDURE insertNewAgencyAndAdmin (IN aName varchar (100), IN numTrav int, IN polType int, IN uEmail varchar(255), IN uFName varchar(40), IN uLName varchar(40), IN uTitle varchar(100))
BEGIN
INSERT INTO btsAgency.Agencies (agencyName, numTrav, polType) VALUES (#aName, #numTrav, #polType);
SET #agencyID = (SELECT agencyID from btsAgency.Agencies where agencyID = LAST_INSERT_ID());
INSERT INTO btsUsers.Users (userAgencyID, userEmail, userFirstName, userLastName, userTitle ) VALUES
(#agencyID, #uEmail, #uFName, #uLName, #uTitle);
END $$
DELIMITER ;
MySql creates the Stored Proc but gives me the following 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 'DELIMITER' at line 1
When I execute the created Stored Proc it still doesn't insert the data in my parameters.
Any help would be appreciated.
Some problems in your stored procedure:
It is important to indicate the difference between 9.4. User-Defined Variables and routine parameters 13.1.15. CREATE PROCEDURE and CREATE FUNCTION Syntax, are different variables (eg.: aName != #aName).
Avoid naming parameters or variables as columns of your tables.
DELIMITER $$
CREATE PROCEDURE `insertNewAgencyAndAdmin` (
/*
IN `aName` varchar (100),
IN `numTrav` int,
IN `polType` int,
IN `uEmail` varchar(255),
IN `uFName` varchar(40),
IN `uLName` varchar(40),
IN `uTitle` varchar(100)
*/
IN `_aName` varchar (100),
IN `_numTrav` int,
IN `_polType` int,
IN `_uEmail` varchar(255),
IN `_uFName` varchar(40),
IN `_uLName` varchar(40),
IN `_uTitle` varchar(100)
)
BEGIN
/*
INSERT INTO `btsAgency`.`Agencies` (
`agencyName`,
`numTrav`,
`polType`
) VALUES (
#`aName`,
#`numTrav`,
#`polType`
);
*/
INSERT INTO `btsAgency`.`Agencies` (
`agencyName`,
`numTrav`,
`polType`
) VALUES (
`_aName`,
`_numTrav`,
`_polType`
);
/*
SET #`agencyID` = (SELECT `agencyID` from `btsAgency`.`Agencies` where `agencyID` = LAST_INSERT_ID());
INSERT INTO `btsUsers`.`Users` (
`userAgencyID`,
`userEmail`,
`userFirstName`,
`userLastName`,
`userTitle`
) VALUES (
#`agencyID`,
#`uEmail`,
#`uFName`,
#`uLName`,
#`uTitle`
);
*/
INSERT INTO `btsUsers`.`Users` (
`userAgencyID`,
`userEmail`,
`userFirstName`,
`userLastName`,
`userTitle`
) VALUES (
LAST_INSERT_ID(),
`_uEmail`,
`_uFName`,
`_uLName`,
`_uTitle`);
END$$
DELIMITER ;
SELECT * FROM discounts $$
delimiter $$;
CREATE PROCEDURE insertData(IN id int,IN title varchar(255),IN amount decimal)
BEGIN
insert into discounts values (id,title,amount);
END; $$
call insertData(3,"sadasd",56);
/*DROP PROCEDURE insertData */

How to declare variables in mysql Stored procedure?

I want to declare a variable and then assign a value of select query to that particular variable and then I need to pass that variable in an insert statement . So I tried the below code,
DROP PROCEDURE IF EXISTS `pro_damagestockdao` $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `pro_damagestockdao`(
IN damage_date date,
IN damage_inv_no varchar(12),
IN damage_inv_date date,
IN damage_dist_name varchar(30),
IN damage_contact_no varchar(15),
IN damage_item_code varchar(30),
IN damage_item_name varchar(30),
IN damage_batch_no varchar(15),
IN damage_mfr_name varchar(50),
IN damage_expiry_date date,
IN damage_pur_qty int(11),
IN damage_qty int(11),
IN damage_unit_price double(10,2),
IN damage_unit_vat double(4,2),
IN damage_unit_discount double(4,2),
IN damage_sub_total double(10,2),
IN damage_total_amount double(10,2),
IN damage_remarks varchar(1000),
IN functionality varchar(20),
out flag int
)
BEGIN
DECLARE selCnt int;
DECLARE dqty int;
SET dqty = (SELECT free FROM purchase_invoice p WHERE p.invoice_no = damage_inv_no);
DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
SET flag=0;
START TRANSACTION;
if(functionality='save') then
INSERT INTO damage_stocks
(damage_stock_date,invoice_no,invoice_date,dist_name,contact_no,item_code,item_name,batch_no,qty,damaged_qty,unit_price,unit_vat,unit_discount,sub_total,total_amount,remarks) VALUES
(damage_date,damage_inv_no,damage_inv_date,damage_dist_name,damage_contact_no,0,damage_item_name,damage_batch_no,dqty,damage_qty,damage_unit_price,damage_unit_vat,damage_unit_discount,damage_sub_total,damage_total_amount,damage_remarks);
But it fails.. You see, I declared a variable dqty and then I passed it to the insert statement.
It is not problem to declaration. It is assign problem
try this query
SELECT free into dqty FROM purchase_invoice p WHERE p.invoice_no = damage_inv_no;
The following code works fine for me:
DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `pro_damagestockdao`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `pro_damagestockdao`(
IN damage_date DATE,
IN damage_inv_no VARCHAR(12),
IN damage_inv_date DATE,
IN damage_dist_name VARCHAR(30),
IN damage_contact_no VARCHAR(15),
IN damage_item_code VARCHAR(30),
IN damage_item_name VARCHAR(30),
IN damage_batch_no VARCHAR(15),
IN damage_mfr_name VARCHAR(50),
IN damage_expiry_date DATE,
IN damage_pur_qty INT(11),
IN damage_qty INT(11),
IN damage_unit_price DOUBLE(10,2),
IN damage_unit_vat DOUBLE(4,2),
IN damage_unit_discount DOUBLE(4,2),
IN damage_sub_total DOUBLE(10,2),
IN damage_total_amount DOUBLE(10,2),
IN damage_remarks VARCHAR(1000),
IN functionality VARCHAR(20),
OUT flag INT)
BEGIN
DECLARE selCnt INT;
DECLARE dqty INT;
DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
SET dqty = (SELECT free FROM purchase_invoice p WHERE p.invoice_no = damage_inv_no);
SET flag = 0;
START TRANSACTION;
IF(functionality = 'save') THEN
INSERT INTO damage_stocks (damage_stock_date, invoice_no, invoice_date, dist_name, contact_no, item_code, item_name, batch_no, qty,damaged_qty, unit_price, unit_vat, unit_discount, sub_total, total_amount, remarks)
VALUES (damage_date, damage_inv_no, damage_inv_date, damage_dist_name, damage_contact_no, 0, damage_item_name, damage_batch_no, dqty,damage_qty, damage_unit_price, damage_unit_vat, damage_unit_discount, damage_sub_total, damage_total_amount, damage_remarks);
END IF;
END$$
DELIMITER ;
With this SP call:
CALL `pro_damagestockdao`("2015-06-23","rx45","2015-06-20","DistName","DContactNo","DamageItemCode","damage_item_name","damage_batch_no","damage_mfr_name", "2015-07-01", 10, 5, 15.2, 7.66, 9.88, 99.00, 150.22, "No remarks", "save", #sorin);
What error do you get?
Do you get an error when trying to create the stored procedure or when you call it?

how to write procedure to insert data in to the table in phpmyadmin?

I have created table as below:
student:
+----+------+-----------+--------+
|uid | name | user_name | branch |
+----+------+-----------+--------+
| | | | |
+----+------+-----------+--------+
I want to insert data in to the table using procedure.
the procedure which i wrote is:
create procedure add(in_name varchar(50),in_user_name
varchar(50),in_branch varchar(50))
begin
insert into student
(name,user_name,branch) values (in_name ,in_user_name,in_branch);
end;
Try this-
CREATE PROCEDURE simpleproc (IN name varchar(50),IN user_name varchar(50),IN branch varchar(50))
BEGIN
insert into student (name,user_name,branch) values (name ,user_name,branch);
END
# Switch delimiter to //, so phpMyAdmin will not execute it line by line.
DELIMITER //
CREATE PROCEDURE usp_rateChapter12
(IN numRating_Chapter INT(11) UNSIGNED,
IN txtRating_Chapter VARCHAR(250),
IN chapterName VARCHAR(250),
IN addedBy VARCHAR(250)
)
BEGIN
DECLARE numRating_Chapter INT;
DECLARE txtRating_Chapter VARCHAR(250);
DECLARE chapterName1 VARCHAR(250);
DECLARE addedBy1 VARCHAR(250);
DECLARE chapterId INT;
DECLARE studentId INT;
SET chapterName1 = chapterName;
SET addedBy1 = addedBy;
SET chapterId = (SELECT chapterId
FROM chapters
WHERE chaptername = chapterName1);
SET studentId = (SELECT Id
FROM students
WHERE email = addedBy1);
SELECT chapterId;
SELECT studentId;
INSERT INTO ratechapter (rateBy, rateText, rateLevel, chapterRated)
VALUES (studentId, txtRating_Chapter, numRating_Chapter,chapterId);
END //
//DELIMITER;
This method work for me:
DELIMITER $$
DROP PROCEDURE IF EXISTS db.test $$
CREATE PROCEDURE db.test(IN id INT(12),IN NAME VARCHAR(255))
BEGIN
INSERT INTO USER VALUES(id,NAME);
END$$
DELIMITER ;