I have created a stored procedure and trying to call it in an event, as I want to execute the stored procedure for every 1 min.
But I couldn't find any result.
This is the stored procedure insert_update_supplier that I wrote
DELIMITER $$
USE `eegpap`$$
DROP PROCEDURE IF EXISTS `insert_update_supplier`$$
CREATE DEFINER=`eegpap`#`%` PROCEDURE `insert_update_supplier`()
BEGIN
SELECT COUNT(*) AS col_no1, Invoice_Date AS invoicedate, No_of_Cases AS d_ncases, No_of_Bottles AS d_nbottles, Product_Code AS d_pcode FROM depot_sales__c WHERE Supplier_Code IN (SELECT Supplier FROM advance_payment_request__c);
SELECT From_Date AS s_fromdate FROM supplier_payment__c;
IF col_no1>0 THEN
IF(CURDATE()-invoicedate>=45) THEN
IF(s_fromdate>invoicedate) THEN
UPDATE supplier_payment__c SET From_Date=invoicedate;
END IF;
IF(d_nbottles!=NULL AND d_nbottles>0) THEN
UPDATE supplier_payment__c SET No_of_Loose_Cases=(No_of_Loose_Cases+1) AND No_of_Loose_Bottles=(No_of_Loose_Bottles+d_nbottles);
END IF;
UPDATE supplier_payment__c SET No_of_Loose_Cases=(No_of_Loose_Cases+1) AND No_of_Cases=(No_of_Cases+d_ncases) AND Cost_Value=(d_ncases*(SELECT Landed_Cost FROM product2 WHERE Supplier_Code=d_pcode));
END IF;
END IF;
END$$
DELIMITER ;
This is the event supplier_event
CREATE EVENT supplier_event
ON SCHEDULE EVERY 5 SECOND
DO
CALL insert_update_supplier();
I don't understand where I made the mistake or where the issue is. Or if there is any other alternative for this other than stored procedure please suggest.
Please some help me out. Thanks in advance
This statement:
SELECT COUNT(*) AS col_no1, Invoice_Date AS invoicedate, No_of_Cases AS d_ncases, No_of_Bottles AS d_nbottles, Product_Code AS d_pcode
FROM depot_sales__c
WHERE Supplier_Code IN (SELECT Supplier FROM advance_payment_request__c);
is not doing what you expect. I think you intend something like:
SELECT col_no1 := COUNT(*),
invoicedate := Invoice_Date,
d_ncases := No_of_Case,
d_nbottles := No_of_Bottles,
d_pcode := Product_Code
FROM depot_sales__c
WHERE Supplier_Code IN (SELECT Supplier FROM advance_payment_request__c);
You should also declare the variables you are using in the code block.
Related
I have two tables DailyVisits and TotalSum My goal is to add DailyVisits to TotalSum with a stored procedure/query that I'll run at the end of the day.
DailyVisits
UserId,PageId,Visits
1,1,32
2,123,34
4,12,213
5,1,1
TotalSum
UserId,PageId,TotalVisits
1,1,300
1,41,2
3,12,213
5,1,653
and so on.
I tried two approaches but I can't get my head around a solution.
Below my queries to achieve this, if you have another suggestion/query simple enough to understand, I appreciate your help.
Approach1:
delimiter $$
CREATE PROCEDURE UPSERT_DAILYSUM()
BEGIN
IF EXISTS (SELECT Id, PageId FROM DailyVisits) THEN
UPDATE TotalSum TotalVisits = TotalVisits + (SELECT Visits FROM DailyVisits);
ELSE INSERT INTO TotalSum (UserId,PageId,TotalVisits)
VALUES (SELECT Id,PageId,Visits);
END IF
END $$
delimiter ;
Approach2:
INSERT INTO TotalSum (UserId,PageId,TotalVisits) VALUES(SELECT * FROM DailyVisits)
ON DUPLICATE KEY UPDATE (PageId,TotalVisits)
VALUES(SELECT PageId,Visits FROM DailyVisits)
This is what I'm struggling with: How will I get distinct values when key does not exist? Can I use RIGHT JOIN (or LEFT JOIN) and check for NULL values, and then add right (or left) table?
same as you need like this:
The problem is i dont know what you want to do in update and insert portion:
delimiter $$
CREATE PROCEDURE UPSERT_DAILYSUM()
BEGIN
Declare idvar int(50) DEFAULT 0;
Declare pageidvar int(50) DEFAULT 0;
SELECT Id, PageId INTO idvar,pageidvar FROM DailyVisits;
if(LENGTH(idvar)>0 THEN
UPDATE TotalSum TotalVisits = TotalVisits + (SELECT Visits FROM DailyVisits);
ELSE
INSERT INTO TotalSum (UserId,PageId,TotalVisits) VALUES (SELECT Id,PageId,Visits);
END $$
delimiter ;
I have the below Stored Procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS spCashDonation$$
CREATE PROCEDURE spCashDonation(IN fname varchar(50),IN lname varchar(50),IN telNo bigint, IN pmode tinyint,IN amt decimal(8,2), OUT rno varchar(20))
BEGIN
Set #rmain := (select trim(concat('DNB', DATE_FORMAT(CURRENT_DATE(), '%y'), DATE_FORMAT(CURRENT_DATE(), '%m'))));
IF ((trim(DATE_FORMAT(CURRENT_DATE(),'%m')) = 01) OR (trim(DATE_FORMAT(CURRENT_DATE(),'%m')) = 1)) THEN
Set #rpart = 1;
END IF;
IF ((trim(DATE_FORMAT(CURRENT_DATE(),'%m')) != 01) OR (trim(DATE_FORMAT(CURRENT_DATE(),'%m')) != 1)) THEN
Set #rpart := (select coalesce(max(ReceiptPart),0) from Donation) + 1;
END IF;
INSERT INTO Donation (ReceiptMain, ReceiptPart, firstName, lastName, telNo, payMode, Amount) VALUES (#rmain, #rpart, fname, lname, telNo, pmode, amt);
Set #lid := (select LAST_INSERT_ID()from donation);
select concat(ReceiptMain,ReceiptPart) into rno from donation where id = #lid;
END$$
DELIMITER ;
Call spCashDonation ('RAJIV','IYER',7506033048,0,1000,#rno);
select #rno;
When the table has no record, the first insert goes through fine. The upon the second insert it throws an error as
Error Code: 1242. Subquery returns more than 1 row
When I query for the last insert id, I get more than 1 value. So, I modified the last part of the procedure to:
Set #lid := (select max(LAST_INSERT_ID()) from donation);
Please advice, if this is fine as it should not hinder any concurrent inserts and future CRUD operations. Thanks in advance.
Set #lid := (select LAST_INSERT_ID() from donation);
In the above line remove the FROM statement. If more than one record in the Donation table it will return the same number of times the LAST_INSERT_ID() value.
So simply use Set #lid := (SELECT LAST_INSERT_ID()); it will work in your case.
I want to make a stored procedure in MySQL that show a data of a table with a row number and not the id of the row. Like this:
And I tried this code:
CREATE DEFINER=`root`#`localhost` PROCEDURE `count_row`()
BEGIN
declare row_num INTEGER;
declare result INTEGER;
SELECT count(id) FROM engineers INTO row_num;
SET result=0;
WHILE(result<=row_num)
DO
SELECT result=result+1, name, date FROM engineers ;
END WHILE;
END
The result was creating undefined number of tabs in MySQL.
Then I tried this code:
CREATE DEFINER=`root`#`localhost` PROCEDURE `count_row`()
BEGIN
declare row_num INTEGER;
declare result INTEGER;
SELECT count(id) FROM engineers INTO row_num;
SET result=1;
WHILE(result<=row_num)
DO
SELECT result, name, date FROM engineers;
SET result = result +1;
END WHILE;
END
And I got 5 tabs (equal to total row number) like this:
second tab:
What I want is to show only one tab with one table as shown in the first picture of the question.
Try
SELECT #row_number:=#row_number+1 as RowNum, name as Engineer, date
FROM engineers, (SELECT #row_number:=0) as t
order by name;
or
SET #row_number:=0;
SELECT #row_number:=#row_number+1 as RowNum, name as Engineer, date
FROM engineers
order by name;
I am writing a procedure to fatch record from database table based on filter condition. Now as user may apply filter or may not. So based on that filter criteria will get applied.
i.e. If filter parameter will contain any value than only it will apply condition not.
Note: I can achieve it through if.. else.. condition and execute different query altogether separately to get my desire output. But what I am trying to ask is, is there any better approach for it rather than if.. else..
Example ( here i_status_id, i_category_id may contain any value or may not)
DELIMITER $$
DROP PROCEDURE IF EXISTS `FilterRecord`$$
CREATE PROCEDURE `FilterRecord`(IN i_status_id BIGINT,IN i_category_id BIGINT)
BEGIN
SELECT
Item_backlog.backlog_id AS backlog_id,
Item_backlog.title AS backlog_name,
Item_backlog_Group.title AS group_Name,
Item_backlog.description AS description,
Item_backlog.est_start_date AS estimated_start_date,
Item_backlog.est_end_date AS estimated_end_date,
Item_backlog.actual_start_date AS actual_start_date,
Item_backlog.actual_end_date AS actual_end_date,
Item_backlog.estimated_hours AS estimated_hours,
Item_backlog.actual_hours AS actual_hours,
Item_backlog.status_id AS status_id,
APV_Status.name AS status_name ,
Item_backlog.priority_id AS priority_id,
APV_Priority.name AS priority_name,
Item_backlog.rank AS rank,
Item_backlog.isActive AS isActive,
Item_backlog.created_by_id AS created_by_id,
Item_backlog.created_on AS created_on,
Item_backlog.modified_by_id AS modified_by_id,
Item_backlog.modified_on AS modified_on
FROM
Item_backlog
INNER JOIN
ApplicationParamValue AS APV_Status ON (Item_backlog.status_id=APV_Status.appParamValueId)
INNER JOIN
ApplicationParamValue AS APV_Priority ON (Item_backlog.Priority_Id=APV_Priority.appParamValueId)
INNER JOIN
Item_backlog_group AS Item_backlog_Group ON (Item_backlog.backlog_group_id=Item_backlog_Group.backlog_group_id)
WHERE
Item_backlog.status_id=i_status_id
AND
Item_backlog.category_id=i_category_id
END$$
DELIMITER ;
Assuming that you have a defined value for indicating when a parameter is not specified (e.g. I've used NULL below to indicate that the parameter is not supplied), what you can do is the following to apply the parameter conditionally.
CREATE PROCEDURE `FilterRecord`(IN i_status_id BIGINT,IN i_category_id BIGINT)
BEGIN
SELECT
Item_backlog.backlog_id AS backlog_id,
-- ...
FROM
Item_backlog
INNER JOIN
-- ..
WHERE
(Item_backlog.status_id=i_status_id OR i_status_id IS NULL)
AND
(Item_backlog.category_id=i_category_id OR i_category_id IS NULL);
END
Note however that the performance of doing this can be degraded.
SqlFiddle example here - I've assumed MySql, btw, as per your primary tag and backticks.
Try below it will apply filter criteria based on parameter passed:
DELIMITER $$
DROP PROCEDURE IF EXISTS `FilterRecord`$$
CREATE PROCEDURE `FilterRecord`(IN i_status_id BIGINT,IN i_category_id BIGINT)
BEGIN
DECLARE Var_status_id VARCHAR(200);
DECLARE Var_category_id VARCHAR(200);
DECLARE andCondition VARCHAR(200);
IF i_status_id<>0 THEN
SET Var_status_id=CONCAT(' (Item_backlog.status_id = ', i_status_id,')');
SET andCondition ='AND';
ELSE
SET Var_status_id="";
SET andCondition ='';
END IF;
IF i_category_id<>0 THEN
SET Var_category_id=CONCAT(andCondition,' (Item_artifact_category_mapping.category_id = ', i_category_id,')');
ELSE
SET Var_category_id="";
END IF;
SET #SQL := CONCAT('SELECT
Item_backlog.backlog_id AS backlog_id,
Item_backlog.title AS backlog_name,
Item_backlog_Group.title AS group_Name,
Item_backlog.description AS description,
Item_backlog.est_start_date AS estimated_start_date,
Item_backlog.est_end_date AS estimated_end_date,
Item_backlog.actual_start_date AS actual_start_date,
Item_backlog.actual_end_date AS actual_end_date,
Item_backlog.estimated_hours AS estimated_hours,
Item_backlog.actual_hours AS actual_hours,
Item_backlog.status_id AS status_id,
APV_Status.name AS status_name ,
Item_backlog.priority_id AS priority_id,
APV_Priority.name AS priority_name,
Item_backlog.rank AS rank,
Item_backlog.isActive AS isActive,
Item_backlog.created_by_id AS created_by_id,
Item_backlog.created_on AS created_on,
Item_backlog.modified_by_id AS modified_by_id,
Item_backlog.modified_on AS modified_on
FROM
Item_backlog
INNER JOIN
ApplicationParamValue AS APV_Status ON (Item_backlog.status_id=APV_Status.appParamValueId)
INNER JOIN
ApplicationParamValue AS APV_Priority ON (Item_backlog.Priority_Id=APV_Priority.appParamValueId)
INNER JOIN
Item_backlog_group AS Item_backlog_Group ON (Item_backlog.backlog_group_id=Item_backlog_Group.backlog_group_id)
WHERE', Var_status_id, Var_category_id);
PREPARE stmt FROM #SQL;
EXECUTE stmt;
END$$
DELIMITER ;
I have a MYSQL table and two of the fields are called Rate_per_unit and Cost. First I want the field Rate_per_unit to populate itself from another table called SHD_TEACHER then I want the field COST to populate itself also from RATE in SHD_TEACHER and multiplies by UNITS.
I have the following code which is giving me an error:
CREATE TRIGGER RATE_PER_UNIT_1
BEFORE INSERT ON SHD_SCHEDULE
FOR EACH ROW
SET NEW.RATE_PER_UNIT =
(
SELECT RATE
FROM SHD_TEACHER
WHERE TEACHERID = NEW.TEACHER_ID
LIMIT 1
)
SET NEW.COST = (
SELECT RATE
FROM SHD_TEACHER
WHERE TEACHERID = NEW.TEACHER_ID
) * UNITS
Any help please?
thanks
using your syntax, I would expect a delimiter statement and a begin/end block. So, try this:
DELIMITER $$
CREATE TRIGGER RATE_PER_UNIT_1
BEFORE INSERT ON SHD_SCHEDULE
FOR EACH ROW
BEGIN
SET NEW.RATE_PER_UNIT =
(
SELECT RATE
FROM SHD_TEACHER t
WHERE t.TEACHERID = NEW.TEACHER_ID
LIMIT 1
)
SET NEW.COST = (
SELECT t.RATE
FROM SHD_TEACHER t
WHERE t.TEACHERID = NEW.TEACHER_ID
) * NEW.UNITS
END $$
DELIMITER ;
You have a limit 1 in the first subquery, suggesting that there might be multiple matches. If so, you will get a run-time error in the second. Also, UNITS is just hanging out there, all alone. I assumed it is in the NEW record.
Here is another way to write this:
DELIMITER $$
CREATE TRIGGER RATE_PER_UNIT_1
BEFORE INSERT ON SHD_SCHEDULE
FOR EACH ROW
BEGIN
SELECT NEW.RATE_PER_UNIT := t.RATE, NEW.COST := t.RATE * NEW.UNITS
FROM (SELECT t.*
FROM SHD_TEACHER t
WHERE t.TEACHERID = NEW.TEACHER_ID
LIMIT 1
) t
END $$
DELIMITER ;