EDIT: I want to replace Appointments.AppointmentTime with tempAppointmentTime.appointmentTime where AppointmentDate = '2022-01-01'. Please see the SQL script at the end to generate the 2 tables.
I have 2 tables: Appointments and tempAppointmentTime, they look like this:
Appointments:
tempAppointmentTime:
I'd like to copy the appointmentTime of tempAppointmentTime and paste to AppointmentTime of Appointments where AppointmentDate = '2022-01-01'. Both of them have 9 rows. I tried many solutions from this post: mysql update column with value from another table
UPDATE Appointments A INNER JOIN tempAppointmentTime T ON A.AppointmentDate = '2022-01-01' SET A.AppointmentTime = T.appointmentTime;
UPDATE Appointments SET Appointments.AppointmentTime =
(SELECT tempAppointmentTime.appointmentTime FROM tempAppointmentTime WHERE tempAppointmentTime.appointmentDate = '2022-01-01');
UPDATE tempAppointmentTime T, Appointments A SET A.AppointmentTime = T.appointmentTime WHERE A.AppointmentDate = '2022-01-01';
None of them worked. Any idea how I can do this?
You can run this script to generate the 2 tables:
DROP TABLE IF EXISTS Appointments;
CREATE TABLE `Appointments` (
`ID` VARCHAR(36) NOT NULL UNIQUE,
`AppointmentDate` DATE,
`AppointmentTime` TIME
);
DROP TABLE IF EXISTS tempAppointmentTime;
CREATE TABLE `tempAppointmentTime` (
`ID` VARCHAR(36) NOT NULL UNIQUE,
`appointmentTime` TIME,
`appointmentDate` DATE,
CONSTRAINT `PK_tempAppointmentTime` PRIMARY KEY (`ID`)
);
DROP PROCEDURE IF EXISTS insertAppointments;
DELIMITER //
CREATE PROCEDURE insertAppointments()
BEGIN
DECLARE i INT;
SET i = 0;
loop_label: LOOP
IF i > 8 THEN
LEAVE loop_label;
END IF;
INSERT INTO Appointments VALUES(UUID(), '2022-01-01', '08:00:00');
SET i = i + 1;
ITERATE loop_label;
END LOOP loop_label;
SET i = 0;
loop_label: LOOP
IF i > 3 THEN
LEAVE loop_label;
END IF;
INSERT INTO Appointments VALUES(UUID(), '2022-01-02', '08:00:00');
SET i = i + 1;
ITERATE loop_label;
END LOOP loop_label;
END //
DELIMITER ;
DROP PROCEDURE IF EXISTS insertTempTime;
DELIMITER //
CREATE PROCEDURE insertTempTime()
BEGIN
DECLARE iterateTimes INT;
DECLARE i INT;
DECLARE totalRows INT;
DECLARE insertTime TIME;
SET iterateTimes = 3;
SET totalRows = 8;
SET i = 0;
SET insertTime = '08:00:00';
loop_label: LOOP
IF i > totalRows THEN
LEAVE loop_label;
END IF;
INSERT INTO tempAppointmentTime VALUES (UUID(), insertTime, '2022-01-01');
SET insertTime = AddTime(insertTime, '00:20:00');
SET i = i + 1;
IF insertTime = '09:00:00' THEN
SET insertTime = '08:00:00';
END IF;
ITERATE loop_label;
END LOOP loop_label;
END //
DELIMITER ;
CALL insertAppointments;
CALL insertTempTime;
I have exported different stored procedures into MySql from Oracle but there is something wrong with this stored procedure, it ends without executing.
I have tried moving data dumps from the tables but it's still not working
CREATE DEFINER = 'root'#'%'
PROCEDURE MOB_INSURANCE.PRC_INITIATE_INSURANCE_PAYMENT(
in PARAM_BEN_CNIC varchar(4000),
in PARAM_INITIATOR_MSISDN varchar(4000),
in PARAM_BENEFICIARY_MSISDN varchar(4000),
in PARAM_INITIATOR_CNIC varchar(4000),
out PARAM_TID varchar(4000),
in PARAM_FEE double,
in PARAM_PREMIUM double,
-- param_plan_id in INTEGER,
in PARAM_COMPANY_CODE varchar(4000),
in PARAM_PRODUCT_CODE varchar(4000),
in PARAM_PLAN_CODE varchar(4000),
in PARAM_EFFECTIVE_DATE datetime,
in PARAM_DURATION double, -- float
in PARAM_TRANS_AMOUNT double, -- flaot
in PARAM_CHANNEL varchar(4000),
in PARAM_CREATED_BY varchar(4000),
in PARAM_CREATED_IP varchar(4000),
out PARAM_RET_CODE varchar(4000),
out PARAM_RET_DESC varchar(4000))
sp_lbl:begin
declare PARAM_EXPIRY DATETIME;
declare COMP_ID DOUBLE;
declare PRODUCT_ID DOUBLE;
-- p_id NUMBER;
declare PLAN_ID DOUBLE;
declare PLAN_DURATION double;
declare PLAN_FEE double;
declare PLAN_PREMIUM double;
declare PLAN_TYPE VARCHAR(50);
declare TRANSACTION_AMOUNT double;
declare PREVIOUSLYPURCHASEDCOUNT DOUBLE;
-- param_newtid VARCHAR2(50 BYTE);
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
begin
rollback;
set PARAM_RET_CODE = '1';
set PARAM_RET_DESC = 'INVALIDINPUT';
-- set PARAM_RET_DESC = CONCAT(IFNULL(SQLERRM, '') , IFNULL(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE, ''));
end;
-- END;
set PARAM_RET_CODE = '2';
-- DBMS_OUTPUT.PUT_LINE('i am here!');
-- param_tid:='495';
-- Input validations
-- Company Code validation
IF PARAM_COMPANY_CODE IS NULL OR PARAM_COMPANY_CODE = '' THEN
sp_lbl1:loop
set PARAM_RET_CODE = '1';
set PARAM_RET_DESC = 'Company Code is required';
set PARAM_TID = '';
Leave sp_lbl1;
end loop sp_lbl1;
ELSE
select('Company Code is valid');
-- Check company code existence in the database
BEGIN
DECLARE EXIT HANDLER FOR NOT FOUND BEGIN
set COMP_ID = NULL;
END;
SELECT ID INTO COMP_ID FROM INSURANCE_COMPANY WHERE CODE = PARAM_COMPANY_CODE AND STATUS = 'Active';
END;
-- Check if company is registered
IF (COMP_ID IS NULL) THEN
sp_lbl2:loop
set PARAM_RET_CODE = '1';
set PARAM_RET_DESC = 'Company Code is not registered/active with the system';
set PARAM_TID = '';
Leave sp_lbl2;
end loop sp_lbl2;
END IF;
END IF;
-- Product Code validation
IF PARAM_PRODUCT_CODE IS NULL OR PARAM_PRODUCT_CODE = '' THEN
sp_lbl3:loop
set PARAM_RET_CODE = '1';
set PARAM_RET_DESC = 'Product Code is required';
set PARAM_TID = '';
Leave sp_lbl3;
end loop sp_lbl3;
ELSE
select(CONCAT('Product Code is valid' , IFNULL(COMP_ID, '') , IFNULL(PARAM_PRODUCT_CODE, '')));
-- Check prodcut code existence in the database
BEGIN
DECLARE EXIT HANDLER FOR NOT FOUND BEGIN
BEGIN
set PRODUCT_ID = NULL;
select('Exception Product Id');
END;
END;
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
BEGIN
set PRODUCT_ID = NULL;
select(CONCAT('Exception Product Id' , IFNULL(SQLERRM, '') , IFNULL(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE, '')));
END;
END;
-- product_id := 0;
-- SELECT p.INSURANCE_PRODUCT_ID INTO product_id FROM INSURANCE_PRODUCTS p WHERE ( p.STATUS = 'Active' AND p.COMPANY_ID = comp_id AND p.product_code = param_product_code); -- AND p.STATUS = 'Active'
SELECT INSURANCE_PRODUCT_ID INTO PRODUCT_ID FROM INSURANCE_PRODUCTS WHERE COMPANY_ID=COMP_ID AND PRODUCT_CODE = PARAM_PRODUCT_CODE AND STATUS = 'Active' ;
select(CONCAT('Product Id : ' , IFNULL(PRODUCT_ID, '')));
-- product_id := 43;
END;
-- Check if product is registered
IF (PRODUCT_ID IS NULL) THEN
sp_lbl4:loop
set PARAM_RET_CODE = '1';
set PARAM_RET_DESC = 'Product Code is not registered/active with the Company code in the system';
set PARAM_TID = '';
Leave sp_lbl4;
end loop sp_lbl4;
END IF;
END IF;
-- Plan Code validation
IF PARAM_PLAN_CODE IS NULL OR PARAM_PLAN_CODE = '' THEN
sp_lbl5:loop
set PARAM_RET_CODE = '1';
set PARAM_RET_DESC = 'Plan Code is required';
set PARAM_TID = '';
Leave sp_lbl5;
end loop sp_lbl5;
ELSE
select('Plan Code is valid');
-- Check prodcut code existence in the database
BEGIN
DECLARE EXIT HANDLER FOR NOT FOUND BEGIN
set PLAN_ID = NULL;
END;
SELECT INSURANCE_PLAN_ID, DURATION INTO PLAN_ID, PLAN_DURATION FROM INSURANCE_PLANS WHERE INSURANCE_PRODUCT_ID = PRODUCT_ID AND PLAN_CODE = PARAM_PLAN_CODE AND STATUS = 'Active';
END;
-- Check if plan is registered
IF (PLAN_ID IS NULL) THEN
sp_lbl6:loop
set PARAM_RET_CODE = '1';
set PARAM_RET_DESC = 'Plan Code is not registered/active with the Product in the system';
set PARAM_TID = '';
Leave sp_lbl6;
end loop sp_lbl6;
END IF;
END IF;
-- Plan Duration Validation
IF PARAM_DURATION IS NULL THEN
sp_lbl6:loop
set PARAM_RET_CODE = '1';
set PARAM_RET_DESC = 'Duration is required';
set PARAM_TID = '';
Leave sp_lbl6;
end loop sp_lbl6;
ELSE
select('Plan Duration is provided');
-- Check if duraiton provided is same defined in database
IF (PLAN_DURATION IS NULL) THEN
sp_lbl7:loop
set PARAM_RET_CODE = '1';
set PARAM_RET_DESC = 'Plan Duration is not defined in the system';
set PARAM_TID = '';
Leave sp_lbl7;
end loop sp_lbl7;
ELSE
IF (PLAN_DURATION != PARAM_DURATION) THEN
sp_lbl8:loop
set PARAM_RET_CODE = '1';
set PARAM_RET_DESC = 'Plan Duration is not the same that is defined in the system';
set PARAM_TID = '';
-- Leave sp_lbl;
leave sp_lbl8;
end loop sp_lbl8;
END IF;
END IF;
END IF;
-- Plan Fee / Premium, transactionAmount validation
IF PARAM_FEE IS NULL THEN
sp_lbl9:loop
set PARAM_RET_CODE = '1';
set PARAM_RET_DESC = 'Plan Fee is required';
set PARAM_TID = '';
Leave sp_lbl9;
end loop sp_lbl9;
END IF;
-- Plan Fee / Premium, transactionAmount validation
IF PARAM_PREMIUM IS NULL THEN
sp_lbl10:loop
set PARAM_RET_CODE = '1';
set PARAM_RET_DESC = 'Plan Premium is required';
set PARAM_TID = '';
Leave sp_lbl10;
end loop sp_lbl10;
END IF;
IF PARAM_PREMIUM IS NOT NULL AND PARAM_FEE IS NOT NULL THEN
select('Plan Fee/Premium is valid');
-- Check plan fee/premium existence in the database
BEGIN
DECLARE EXIT HANDLER FOR NOT FOUND BEGIN
sp_lbl11:loop
set PLAN_FEE = NULL;
set PLAN_PREMIUM = NULL;
set PLAN_TYPE = NULL;
leave sp_lbl11;
end loop sp_lbl11;
END;
SELECT FEE, PREMIUM,FEETYPE INTO PLAN_FEE, PLAN_PREMIUM,PLAN_TYPE FROM PLAN_PRICING WHERE INSURANCE_PLAN_ID = PLAN_ID AND END_TIME IS NULL;
END;
-- Check if plan is registered
IF (PLAN_FEE IS NULL) THEN
sp_lbl12:loop
set PARAM_RET_CODE = '1';
set PARAM_RET_DESC = 'Plan Fee is not defined in the system';
set PARAM_TID = '';
Leave sp_lbl12;
end loop sp_lbl12;
END IF;
-- Check if plan is registered
IF (PLAN_PREMIUM IS NULL) THEN
sp_lbl13:loop
set PARAM_RET_CODE = '1';
set PARAM_RET_DESC = 'Plan Premium is not defined in the system';
set PARAM_TID = '';
Leave sp_lbl13;
end loop sp_lbl13;
END IF;
-- Check if plan is registered
IF (PLAN_TYPE IS NULL) THEN
sp_lbl14:loop
set PARAM_RET_CODE = '1';
set PARAM_RET_DESC = 'Plan Fee type is not defined in the system';
set PARAM_TID = '';
Leave sp_lbl14;
end loop sp_lbl14;
END IF;
-- Check if plan premium is registered
IF (PLAN_PREMIUM != PARAM_PREMIUM) THEN
sp_lbl15:loop
set PARAM_RET_CODE = '1';
set PARAM_RET_DESC = 'Plan Premium is not the same which is defined in the system';
set PARAM_TID = '';
Leave sp_lbl15;
end loop sp_lbl15;
END IF;
-- Check if plan fee is registered
IF (PLAN_FEE != PARAM_FEE) THEN
sp_lbl16:loop
set PARAM_RET_CODE = '1';
set PARAM_RET_DESC = 'Plan Fee is not the same which is defined in the system';
set PARAM_TID = '';
Leave sp_lbl16;
end loop sp_lbl16;
END IF;
-- Check if trnsaction amount is sum of fee and premium
IF PLAN_TYPE = 'Percentage Amount' THEN
-- Plan fee is percentage of premium
set PLAN_FEE = (PLAN_FEE/100) * PLAN_PREMIUM;
END IF;
IF ((PLAN_PREMIUM + PLAN_FEE) != PARAM_TRANS_AMOUNT) THEN
sp_lbl17:loop
set PARAM_RET_CODE = '1';
set PARAM_RET_DESC = 'Sum of Fee and Premium is not equal to transaction amount provided.';
set PARAM_TID = '';
Leave sp_lbl17;
end loop sp_lbl17;
END IF;
END IF; -- outer if condition for param_fee
-- Check if CNIC has already subscribed this plan
SELECT COUNT(*) into PREVIOUSLYPURCHASEDCOUNT
FROM PURCHASED_INSURANCE
WHERE BENEFICIARY_MSISDN=PARAM_BENEFICIARY_MSISDN AND INSURANCE_PLAN_ID=PLAN_ID AND EXPIRY_DATE>NOW() AND STATUS!='Refunded' AND STATUS!='Unpaid';
select(CONCAT('Previously Purchased Count ' , IFNULL(PREVIOUSLYPURCHASEDCOUNT, '')));
IF (PREVIOUSLYPURCHASEDCOUNT > 0) THEN
sp_lbl18:loop
set PARAM_RET_CODE = '1';
set PARAM_RET_DESC = 'The plan has already been subscribed by the beneficiary MSISDN';
set PARAM_TID = '';
Leave sp_lbl18;
end loop sp_lbl18;
END IF;
SELECT SEQ_TRANSACTION.nextval INTO PARAM_TID FROM DUAL;
-- DBMS_OUTPUT.PUT_LINE('PARAM_RET_DESC = ' || PARAM_RET_DESC);
-- DBMS_OUTPUT.PUT_LINE('PARAM_RET_DESC = ' || param_tid);
SELECT (PARAM_EFFECTIVE_DATE+PARAM_DURATION) INTO PARAM_EXPIRY FROM DUAL;
INSERT INTO PURCHASED_INSURANCE (TID,FEE,EFFECTIVE_DATE,EXPIRY_DATE,PREMIUM,BENEFICIARY_CNIC,
INITIATOR_MSISDN,INSURANCE_PLAN_ID,BENEFICIARY_MSISDN,INITIATOR_CNIC,CHANNEL, CREATED_BY,CREATED_IP,CREATED_DATETIME,STATUS,TRANSACTION_AMOUNT)
VALUES(PARAM_TID,PARAM_FEE,PARAM_EFFECTIVE_DATE,PARAM_EXPIRY,PARAM_PREMIUM,PARAM_BEN_CNIC,
PARAM_INITIATOR_MSISDN,PLAN_ID,PARAM_BENEFICIARY_MSISDN,PARAM_INITIATOR_CNIC,PARAM_CHANNEL,PARAM_CREATED_BY, PARAM_CREATED_IP,SYSDATE(),'Unpaid',PARAM_TRANS_AMOUNT);
INSERT INTO TRANSACTION_HISTORY (TID,START_TIME,END_TIME,STATUS,TRANSACTION_AMOUNT,CREATED_BY,CREATED_IP,CREATED_DATETIME)
VALUES(PARAM_TID,PARAM_EFFECTIVE_DATE,NULL,'Unpaid',PARAM_TRANS_AMOUNT,PARAM_CREATED_BY, PARAM_CREATED_IP,SYSDATE()) ;
COMMIT;
set PARAM_RET_CODE = '0';
-- leave sp_lbl;
END;
end
I just see no value in the result but if it's working then it will return 01 with invalid input in exception case and 00 with transaction id in working case
Didnt try running it but by just looking at it I can see that there are 2 variables which are storing what is happening in your stored proc.
out PARAM_RET_CODE varchar(4000)
out PARAM_RET_DESC varchar(4000)
These 2 variables are the actual output of your stored proc.
Whichever may be the condition, but by printing these 2 variables will give you some insights into what is that condition because of which this stored proc is not returning anything. Print them at the end of the stored proc, you will get your answer.
Actually I'm trying to insert some data into a table using a cursor, and I'm getting no error. It just says:
Query executed OK, 0 rows affected.
I'm trying to get data from each field of the testdatabase.wp_posts p table and insert it into an existing table `testdatabase.wp_postmeta.
My syntax is:
DROP PROCEDURE IF EXISTS add_authors_to_prod;
DELIMITER $$
CREATE PROCEDURE add_authors_to_prod()
BEGIN
DECLARE v_post_id INT;
DECLARE v_author_id INT;
DECLARE v_last_author INT DEFAULT 0;
DECLARE str VARCHAR(500);
DECLARE v_first_id INT DEFAULT 10916;
DECLARE author_csr CURSOR FOR
SELECT n.nid, p.id FROM testdatabase.wp_posts p
JOIN drupaltest.srmaif_field_data_field_auther_list auth ON auth.entity_id = p.id
JOIN drupaltest.node_field_data n ON auth.field_auther_list_nid = n.nid
ORDER BY p.id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_last_author = 1;
START TRANSACTION;
OPEN author_csr;
author_loop:LOOP
FETCH author_csr INTO v_author_id, v_post_id;
IF v_last_author THEN
SET str = CONCAT(str, '}');
LEAVE author_loop;
END IF;
IF v_first_id <> v_post_id THEN
SET v_first_id = v_post_id;
SET str = CONCAT(str, '}');
INSERT INTO testdatabase.wp_postmeta (post_id, meta_key, meta_value)
VALUES(v_post_id, 'authors', CONCAT(str, v_author_id));
SET str = 'a:2:{';
ELSE
SET str = CONCAT(str,'i:', v_author_id, ';s:5:\"', v_author_id, '\";');
END IF;
END LOOP author_loop;
CLOSE author_csr;
SET v_last_author=0;
END$$
DELIMITER ;
CALL add_authors_to_prod()
I am creating Trigger into Mysql Workbanch , but not able to create the trigger.
Can Anyone Help me For This.
DELIMITER $$
create TRIGGER Insert_Test
After Delete ON Test
FOR EACH ROW
BEGIN
START TRANSACTION;
DECLARE v_ID INT;
DECLARE v_Error TINYINT;
DECLARE v_AgentID INT;
SELECT v_Error = 0;
v_ID=Old.id;
BEGIN
DELETE Test2 WHERE id = SELECT id FROM Test where id=v_ID;
Rollback;
SET v_Error = 1;
END
IF v_Error = 0;
THEN
COMMIT;
ELSEIF
v_Error = 1;
THEN
ROLLBACK;
END IF;
END
DELIMITER ;
Sql server Trigger
ALTER TRIGGER [dbo].[tr_DelRecordTypeID] ON [dbo].[luRecordType] FOR DELETE
AS
SET NOCOUNT ON
BEGIN TRANSACTION
DECLARE #ID INT, #GroupTypeID INT, #Error BIT, #Msg VARCHAR(500)
SELECT #Error = 0
SELECT #ID = RecordTypeID FROM deleted
SELECT #GroupTypeID = 30
IF EXISTS ( SELECT g.GroupID
FROM luGroup g,
[luGroupDetail] gd
WHERE g.[GroupID] = gd.[GroupID]
AND g.[GroupTypeID] = #GroupTypeID
AND gd.[MemberID] = #ID )
BEGIN
DELETE [agAgent] WHERE [AgentID] = (SELECT TOP 1 AgentID FROM agAgentPayType)
Rollback transaction
SET #Error = 1
END
IF #Error = 0
BEGIN
COMMIT TRANSACTION
END
ELSE
IF #Error = 1
BEGIN
ROLLBACK TRANSACTION
END
This trigger which i am trying to achieve into mysql workbanch, please check
I shall Be thankful,
Thanks
Aman
I am making a procedure that inserts a place ("Sted") and I would like to check if the inputs are NULL. However, whenever I try to add an if-statement at the start to surround my code (marked CRASH below), it gives me an error saying my syntax is not right at "DECLARE varStedskodeID INT;" which is the part after the IF-statement I'm trying to add.
To my eyes the syntax of my if-statement is the same inside the code, but only my soon-to-be-NULL-check if-statement crashes even with just a simple IF(TRUE) THEN.
Can anyone give me a hint of what causes this one if to crash?
DROP PROCEDURE IF EXISTS InsertSted;
DELIMITER $$
CREATE PROCEDURE InsertSted(
IN inputStedsnavn VARCHAR(255),
IN inputStedstype VARCHAR(255),
IN inputKommunenavn VARCHAR(255))
BEGIN
IF(TRUE) THEN <<------ CRASH
DECLARE varStedskodeID INT;
DECLARE varKommunenr INT;
IF(SELECT COUNT(StedkodeID) FROM stedstype WHERE Kodenavn = inputStedstype LIMIT 1) = 0 THEN
INSERT INTO stedstype VALUES(DEFAULT, inputStedstype);
END IF;
SET varStedskodeID = (SELECT StedkodeID FROM stedstype WHERE Kodenavn = inputStedstype LIMIT 1);
IF(SELECT COUNT(Kommunenr) FROM kommune WHERE Kommunenavn = inputKommunenavn LIMIT 1) = 1 THEN
SET varKommunenr = (SELECT Kommunenr FROM kommune WHERE Kommunenavn = inputKommunenavn LIMIT 1);
INSERT INTO sted VALUES(DEFAULT, inputStedsnavn, varStedskodeID, varKommunenr);
END IF;
END IF; <<------ CRASH
END$$
DELIMITER ;
DECLARE is permitted only inside a BEGIN ... END compound statement
and must be at its start, before any other statements.
http://dev.mysql.com/doc/refman/5.0/en/declare.html
MySQL follows strict rules for DECLARE. You have to DECLARE variables, tables, etc... at the beginning of Stored Procedure.
Change Stored Procedure like this
DECLARE varStedskodeID INT;
DECLARE varKommunenr INT;
IF(TRUE) THEN
IF(SELECT COUNT(StedkodeID) FROM stedstype WHERE Kodenavn = inputStedstype LIMIT 1) = 0 THEN
INSERT INTO stedstype VALUES(DEFAULT, inputStedstype);
END IF;
SET varStedskodeID = (SELECT StedkodeID FROM stedstype WHERE Kodenavn = inputStedstype LIMIT 1);
IF(SELECT COUNT(Kommunenr) FROM kommune WHERE Kommunenavn = inputKommunenavn LIMIT 1) = 1 THEN
SET varKommunenr = (SELECT Kommunenr FROM kommune WHERE Kommunenavn = inputKommunenavn LIMIT 1);
INSERT INTO sted VALUES(DEFAULT, inputStedsnavn, varStedskodeID, varKommunenr);
END IF;
END IF;