I got this error message:
Result consisted of more than one row
but I check my procedure again and again but I cannot find any problem:
DROP PROCEDURE IF EXISTS SP_Fetch_Returned_With_Serials;
CREATE PROCEDURE `SP_Fetch_Returned_With_Serials`()
BEGIN
DECLARE Num BIGINT UNSIGNED DEFAULT 0;
DECLARE __Product_Id bigint UNSIGNED DEFAULT NULL;
DECLARE __Serials varchar(255) DEFAULT NULL;
DECLARE done INT DEFAULT 0;
DECLARE cur CURSOR FOR
SELECT product_id, serials FROM tmp_table_returned_product_serial;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
START TRANSACTION ;
SET #Document_Id = 0 ;
SET #Product_Id = 0;
SET #Payment_Amount = 0;
OPEN cur;
read_loop:
LOOP
IF done
THEN
LEAVE read_loop;
END IF;
FETCH cur INTO __Product_Id,__Serials;
CALL SP_Separate_Numeric_Values(`__Serials`);
SET #Is_Returnable = 0;
SET #Level_Id = 0;
SET #Free_Day = 0;
SET #Penalty_Percent = 0;
SET #Factor_Type_Id = 0;
SET #Currency_Id = 0;
SET #Customer_Id = 0;
SET #Factor_Id = 0;
SET #Value = 0;
SET #Real_Fee = 0;
SET #Date_At = CURRENT_DATE();
SET #Diff_Days = 0;
SET #New_Factor_Id = 0;
SET #New_Detail_Factor_Id = 0;
SET #Receipt_Remit_Type_Id = 0;
SET #Return_Penalty_Percent = 0;
SET #Return_Penalty_Price = 0;
SET #Document_Number = 0;
SELECT id INTO #Factor_Type_Id FROM Tb_Factor_Types WHERE name = 'back_sale_factor';
SELECT COUNT(NV.Number) INTO #Value FROM Numeric_Values NV;
SELECT TP.is_returnable,
TU.level_id,
currency_id,
customer_id,
VF.id,
VF.product_id,
real_fee,
date_at
INTO #Is_Returnable,#Level_Id,#Currency_Id,#Customer_Id,#Factor_Id,#Product_Id,#Real_Fee,#Date_At
FROM Vw_Factor_Master_Details VF
INNER JOIN Tb_Factor_Detail_Serials TFDS ON TFDS.detail_id = VF.detail_id
INNER JOIN Tb_Products TP ON VF.product_id = TP.id
INNER JOIN Tb_Users TU ON TU.user_id = VF.customer_id
INNER JOIN Numeric_Values NV ON NV.Number = TFDS.serial_id;
IF (#Is_Returnable)
THEN
SET #Product_Title = '';
SET #Error_Msg = '';
SELECT IFNULL(TPT.title, TP.title_en) AS title INTO #Product_Title FROM Tb_Products TP
LEFT JOIN (SELECT title, product_id FROM Tb_Product_Translations WHERE locale = #Locale) TPT
ON TP.id = TPT.product_id WHERE product_id = #Product_Id;
SELECT message INTO #Error_Msg FROM Tb_Errors WHERE error_code = 1000028 AND locale = #Locale;
SET #Error_Msg = REPLACE(#Error_Msg, ':product', #Product_Title);
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = #Error_Msg;
END IF;
SELECT DATEDIFF(CURRENT_DATE(), #Date_At) INTO #Diff_Days;
SELECT MAX(`number`) INTO Num FROM Tb_Factors WHERE year_id = #Fiscal_Year AND type = 'sale_factor';
SET Num = ifnull(Num, 0) + 1;
SELECT JSON_EXTRACT(JSON_EXTRACT(TS.value, CONCAT('$.', #Level_Id)), '$.free_day')
INTO #Free_Day
FROM Tb_Settings TS
WHERE `key` = 'returned_product';
SELECT JSON_EXTRACT(JSON_EXTRACT(TS.value, CONCAT('$.', #Level_Id)), '$.penalty_percent')
INTO #Penalty_Percent
FROM Tb_Settings TS
WHERE `key` = 'returned_product';
IF (#Diff_Days > #Free_Day)
THEN
SET #Payment_Amount = #Payment_Amount + ((#Real_Fee - (#Real_Fee * #Penalty_Percent / 100)) * #Value);
SET #Return_Penalty_Percent = #Penalty_Percent;
SET #Return_Penalty_Price = #Real_Fee * #Penalty_Percent / 100;
END IF;
IF(#New_Factor_Id = 0)
THEN
INSERT INTO Tb_Factors (type, sale_place, product_type, company_id, branch_id, cash_desk_id, type_id, year_id,
currency_id, finaler_id, signature_id, customer_id, final_at, signature_at,
reference_factor_id, creator_id, number)
VALUES ('sale_factor', 'branch', 'product', #Company, #Branch_Id, NULL, #Factor_Type_Id, #Fiscal_Year,
#Currency_Id, #Auth_User, #Auth_User, #Customer_Id, CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP(),
#Factor_Id, #Auth_User, Num);
SET #New_Factor_Id = LAST_INSERT_ID();
END IF;
INSERT INTO Tb_Factor_Details (product_id, value, real_fee, fee, factor_id, creator_id, return_penalty_percent,
return_penalty_price)
VALUES (#Product_Id, #Value, #Real_Fee, #Real_Fee, #New_Factor_Id, #Auth_User, #Penalty_Percent,
#Return_Penalty_Price);
SET #New_Detail_Factor_Id = LAST_INSERT_ID();
INSERT INTO Tb_Factor_Detail_Serials (serial_id, detail_id)
SELECT NV.Number, #New_Detail_Factor_Id FROM Numeric_Values NV;
SELECT id INTO #Receipt_Remit_Type_Id FROM Tb_Receipt_Remit_Types WHERE name = 'return_of_sales';
SET #Warehouse_Id = 0;
SELECT id INTO #Warehouse_Id FROM Tb_Warehouses WHERE warehouse_branch_id = #Branch_Id LIMIT 1;
IF(#Document_Id = 0)
THEN
SELECT document_number
FROM Tb_Documents
WHERE fiscal_year_id = #Fiscal_Year
ORDER BY document_number DESC
LIMIT 1
INTO #Document_Number;
SET #Document_Number = #Document_Number + 1;
INSERT INTO Tb_Documents (type, factor_id, warehouse_id, receipt_remit_type_id, fiscal_year_id, document_number,
creator_id)
VALUES ('receipt', #New_Factor_Id, #Warehouse_Id, #Receipt_Remit_Type_Id, #Fiscal_Year, #Document_Number,
#Auth_User);
SET #Document_Id = LAST_INSERT_ID();
END IF;
INSERT INTO Tb_Document_Details (product_id, value, confirmed_at, confirmed_by, creator_id, document_id)
VALUES (#Product_Id, #Value, CURRENT_TIMESTAMP(), #Auth_User, #Auth_User, #Document_Id);
SET #Document_Detail_Id = LAST_INSERT_ID();
INSERT INTO Tb_Detail_Serial_Numbers (serial_number_id, detail_id, origin_cost_center_id, confirmed_at,
confirmed_by)
SELECT NV.Number, #Document_Detail_Id,NULL,CURRENT_TIMESTAMP(),#Auth_User FROM Numeric_Values NV;
END LOOP;
CLOSE cur;
SET #Type_Id = 0;
SELECT id INTO #Type_Id FROM Tb_Payment_Types WHERE name = 'cash' LIMIT 1;
INSERT INTO Tb_Receive_Payment (model_type, model_id, factor_id, type_id, receive_amount, payment_amount,year_id,creator_id)
VALUES ('cashdesk', NULL, #New_Factor_Id, #Type_Id, 0, #Payment_Amount,#Fiscal_Year,#Auth_User);
SET #Payment_Id = LAST_INSERT_ID();
SELECT #Factor_Id as factor_id,#Payment_Id as payment_id,#Payment_Amount as payment_amount;
COMMIT;
END
guys!
I have a trigger in MySQL database:
CREATE DEFINER="root"#"127.0.0.1" TRIGGER `tai_actions_for_active_count` AFTER INSERT ON `actions` FOR EACH ROW BEGIN
DECLARE l_isnn TINYTEXT;
IF NEW.action_type IN ('CREATION', 'VERIFICATION', 'CLOSE') THEN
SET l_isnn = IF(NEW.isnn is NULL, '*', NEW.isnn);
IF NOT NEW.action_type = 'CLOSE' THEN
INSERT INTO subscriptions.`statistics_active_count`(`service_id`, `operator_id`, `isnn`, `active_count`)
VALUES (NEW.service_id, NEW.operator_id, l_isnn, 1)
ON DUPLICATE KEY UPDATE active_count = active_count + 1;
ELSE
SET #tai_actions_for_active_count = -1;
UPDATE subscriptions.`statistics_active_count` SET active_count = #tai_actions_for_active_count := active_count - 1
WHERE `service_id` = NEW.service_id AND `operator_id` = NEW.operator_id AND `isnn` = l_isnn;
IF #tai_actions_for_active_count = 0 THEN DELETE FROM subscriptions.`statistics_active_count` WHERE `active_count` = 0; END IF;
END IF;
END IF;
END
So I need to rewrite it to make it works in Postgres database. As there's ON DUPLICATE KEY UPDATE I'm using Postgres version 9.5 with UPSERT (ON CONFLICT (KEY) DO UPDATE).
So I poorly know SQL language can you tell me what I'm doing wrong? There's the Postgres PL code:
DECLARE
l_isnn TEXT;
tai_actions_for_active_count INTEGER;
BEGIN
IF NEW.action_type IN ('CREATION', 'VERIFICATION', 'CLOSE') THEN
IF NEW.isnn is NULL THEN
l_isnn := '*';
ELSE
l_isnn := NEW.isnn;
END IF;
IF NOT NEW.action_type = 'CLOSE' THEN
INSERT INTO "subscriptions.statistics_active_count"(service_id, operator_id, isnn, active_count)
VALUES (NEW.service_id, NEW.operator_id, l_isnn, 1)
ON CONFLICT(active_count) DO UPDATE SET active_count = active_count + 1;
ELSE
tai_actions_for_active_count := -1;
UPDATE "subscriptions.statistics_active_count" SET active_count = active_count - 1
-- (tai_actions_for_active_count := active_count - 1)
WHERE service_id = NEW.service_id AND operator_id = NEW.operator_id AND isnn = l_isnn;
UPDATE "subscriptions.statistics_active_count" SET tai_actions_for_active_count = active_count
WHERE service_id = NEW.service_id AND operator_id = NEW.operator_id AND isnn = l_isnn;
IF tai_actions_for_active_count = 0 THEN DELETE FROM "subscriptions.statistics_active_count" WHERE active_count = 0; END IF;
END IF;
END IF;
RETURN NULL;
END;
As I want to test this trigger I'm getting an error -- relation "subscriptions.statistics_active_count" does not exist
Can you help me with that code?
Finally I've got the solution. I guess :)
BEGIN
IF NEW.action_type IN ('CREATION', 'VERIFICATION', 'CLOSE') THEN
IF NEW.isnn IS NULL THEN
l_isnn := '*';
ELSE
l_isnn := NEW.isnn;
END IF;
IF NOT NEW.action_type = 'CLOSE' THEN
BEGIN
INSERT INTO subscriptions.statistics_active_count (service_id, operator_id, isnn, active_count)
VALUES (NEW.service_id, NEW.operator_id, l_isnn, 1);
EXCEPTION WHEN unique_violation THEN
UPDATE subscriptions.statistics_active_count SET active_count = active_count + 1
WHERE service_id = NEW.service_id and operator_id=NEW.operator_id;
END;
ELSE
tai_actions_for_active_count := -1;
WITH upd AS
(UPDATE subscriptions.statistics_active_count
SET active_count = active_count - 1
WHERE service_id = NEW.service_id AND operator_id = NEW.operator_id AND isnn = l_isnn;
RETURNING active_count)
SELECT *
FROM upd INTO tai_actions_for_active_count;
IF tai_actions_for_active_count = 0 THEN
DELETE FROM public.statistics_active_count
WHERE active_count = 0;
END IF;
END IF;
END IF;
END;
I have the event
CREATE EVENT `start_insert_record_to_inout` ON SCHEDULE EVERY 10 SECOND STARTS '2014-07-12 12:07:00'
ON COMPLETION PRESERVE
ENABLE
COMMENT 'comment'
DO BEGIN
DECLARE `_count` INT DEFAULT 0;
DECLARE `_done` INT DEFAULT 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET _done = 1;
SELECT `count` INTO `_count`
FROM state_execution
WHERE name = 'insert_record_to_inout' AND `type` = 'event'
LIMIT 1;
-- AND `type` = 'event';
IF _done THEN
INSERT INTO state_execution (`name`, `type`, `isUsed`,`count`) VALUES ('insert_record_to_inout', 'event','0',0);
END IF;
IF `_count` < 50 THEN
IF _done THEN
SET `_count` = 1;
SET _done = 0;
ELSE
SET `_count` = `_count` + 1;
END IF;
UPDATE state_execution SET `count` = `_count`
WHERE name = 'insert_record_to_inout' AND `type` = 'event';
call insert_record_to_inout();
SELECT `count` INTO `_count`
FROM state_execution
WHERE name = 'insert_record_to_inout' AND `type` = 'event'
LIMIT 1;
IF _done THEN
SET `_count` = 0;
ELSE
SET `_count` = `_count` - 1;
END IF;
UPDATE state_execution SET `count` = `_count` WHERE name = 'insert_record_to_inout' AND `type` = 'event';
END IF;
END
But this code
SELECT `count` INTO `_count`
FROM state_execution
WHERE name = 'insert_record_to_inout' AND `type` = 'event'
LIMIT 1;
not working. Please help me, thanks.
I created a trigger in mysql, it's no compilation error, but does nothing of what was proposed in it, is not the same as it was called.
Following code.
DELIMITER //
DROP TRIGGER BILHETAGEM_ACERTOS//
CREATE TRIGGER BILHETAGEM_ACERTOS
BEFORE INSERT ON bilhetagem FOR EACH ROW
BEGIN
SET new.bil_canal = LEFT(new.bil_canal,locate('-',new.bil_canal)-1);
SET #plano = 0;
SET #tarifa = 0;
SET #placa = 0;
SET #seq = 0;
SET #canal = 0;
SET #tronco = 0;
SET #troncoplaca = 0;
SET new.bil_gravacao = SUBSTRING(new.bil_gravacao,7);
SELECT SUBSTRING(new.bil_canal, 1, (LOCATE('/', new.bil_canal) -1)) INTO #placa;
IF (#placa = 'Khomp') THEN
SET #canal = RIGHT(new.bil_canal,LENGTH(new.bil_canal) - LOCATE('/', new.bil_canal));
SET #seq = LEFT(#canal, LOCATE('C', #canal) -1);
IF (LOCATE('-', #canal) > 0) THEN
SET #canal = LEFT(#canal, LOCATE('-', #canal) -1);
END IF;
SET #canal = RIGHT(#canal,(LENGTH(#canal) - LOCATE('C', #canal)));
SELECT tro_codigo INTO #tronco FROM troncos_placas where tropla_placa = #seq LIMIT 1;
SELECT tro_placa INTO #tipo FROM troncos where tro_codigo = #tronco LIMIT 1;
SELECT tro_nome INTO #tronco_nome FROM troncos where tro_codigo = #tronco LIMIT 1;
IF (#tipo = 'E1') THEN
SELECT tropla_codigo INTO #troncoplaca FROM troncos_placas where tro_codigo = #tronco LIMIT 1;
ELSE
SELECT tro_codigo INTO #tronco FROM troncos_placas where tropla_placa = #seq and tropla_codigo = #canal LIMIT 1;
SET #troncoplaca = #canal;
END IF;
ELSE
SELECT SUBSTRING(new.bil_canal, (LOCATE('/', new.bil_canal) +1)) INTO #canal;
SELECT tro_codigo INTO #tronco FROM troncos where tro_tipo = #placa and tro_apelido = #canal LIMIT 1;
SET #troncoplaca = 0;
END IF;
IF (LOCATE('B', new.ram_codigo)) THEN
SET new.ram_codigo = #tronco_nome;
END IF;
IF (LOCATE('GATW', new.ram_codigo)) THEN
SET new.ram_codigo = #tronco_nome;
END IF;
SET new.tro_codigo = #tronco;
SET new.tropla_codigo = #troncoplaca;
SELECT pla_codigo INTO #plano FROM ramal where ram_codigo = new.ram_codigo LIMIT 1;
IF #plano > 0 THEN
IF ((LEFT(new.bil_destino,1) > 0) and (LEFT(new.bil_destino,1) <= 5)) THEN
SELECT pla_vlrminutolocalfixo INTO #tarifa FROM planos where pla_codigo = #plano LIMIT 1;
ELSE
IF ((LEFT(new.bil_destino,1) > 5) and (LEFT(new.bil_destino,1) <= 9)) THEN
SELECT pla_vlrminutolocalcelular INTO #tarifa FROM planos where pla_codigo = #plano LIMIT 1;
ELSE
IF (LEFT(new.bil_destino,4) <> '0800') THEN
IF ((SUBSTRING(new.bil_destino,2,1) < 2) and (SUBSTRING(new.bil_destino,4,1) <= 5)) THEN
SELECT pla_vlrminutodddfixo_deestado INTO #tarifa FROM planos where pla_codigo = #plano LIMIT 1;
ELSE
SELECT pla_vlrminutodddfixo_foestado INTO #tarifa FROM planos where pla_codigo = #plano LIMIT 1;
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
SET new.pla_codigo = #plano;
SELECT pla_tempo INTO #tempo FROM planos where pla_codigo = #plano LIMIT 1;
SELECT pla_cadencia INTO #cadencia FROM planos where pla_codigo = #plano LIMIT 1;
SET new.pla_tempo = #tempo;
SET new.pla_cadencia = #cadencia;
SET new.bil_tarifabase = #tarifa;
SET new.bil_tarifado = 'S';
SET #relseg = new.bil_duracao;
SET #reldata = DATE(new.bil_dataligacao);
IF ((LENGTH(new.bil_destino) > 4) AND (new.bil_duracao > 0) AND (new.bil_entradasaida <> 'E')) THEN
IF (new.bil_duracao <= #tempo) THEN
SET new.bil_vlrvenda = (#tarifa / 60) * #tempo;
ELSE
SET #valor = (#tarifa / 60) * #cadencia;
SET #tmp = (new.bil_duracao - #tempo);
SET #qtd = 1;
WHILE #tmp > 0 DO
SET #tmp = (#tmp - #cadencia);
SET #qtd = #qtd + 1;
END WHILE;
SET new.bil_vlrvenda = #tarifa + (#valor * #qtd);
END IF;
END IF;
IF (#tronco > 0) THEN
SELECT reltro_segundos INTO #segundos from relatorio_troncos where tro_codigo = #tronco and reltro_dataligacao = #reldata limit 1;
IF ((#segundos > 0) or (#segundos is not null)) THEN
UPDATE relatorio_troncos SET reltro_segundos = (#segundos + #relseg) WHERE tro_codigo = #tronco and reltro_dataligacao = new.bil_dataligacao;
ELSE
INSERT INTO relatorio_troncos (tro_codigo, reltro_dataligacao, reltro_segundos) values (#tronco, #reldata, #relseg);
END IF;
END IF;
END IF;
END//
DELIMITER ;``
I am in need of mysql function but gives same value every time
this mysql function gives me same value every time i.e. S_Start_ToBeStarted (Value of 1st If)
-- colorA
DELIMITER $$
DROP FUNCTION IF EXISTS `colorA`$$
CREATE FUNCTION `colorA`(
Appf VARCHAR(3),
Start_Datef DATETIME,
PDCf DATETIME
) RETURNS INT
DETERMINISTIC
BEGIN
DECLARE finalcolor INT DEFAULT 0;
IF Appf = 'Yes' & IFNULL(Start_Datef, 'NULL' = 'NULL' & sysdate() <= PDCf
THEN SET finalcolor = (SELECT `PP_Colors`.`S_Start_ToBeStarted` FROM `SCR_Sap`.`PP_Colors` LIMIT 1);
ELSE IF Appf = 'Yes' & IFNULL(Start_Datef, 'NULL') = 'NULL' & sysdate() > PDCf
THEN SET finalcolor = (SELECT `PP_Colors`.`S_Start_Error` FROM `SCR_Sap`.`PP_Colors` LIMIT 1);
ELSE IF Appf = 'Yes' & IFNULL(Start_Datef, 'NULL') != 'NULL' & sysdate() <= PDCf
THEN SET finalcolor = (SELECT `PP_Colors`.`S_Start_Ok` FROM `SCR_Sap`.`PP_Colors` LIMIT 1);
ELSE IF Appf = 'Yes' & IFNULL(Start_Datef, 'NULL') != 'NULL' & sysdate() > PDCf
THEN SET finalcolor = (SELECT `PP_Colors`.`S_Start_LateStarted` FROM `SCR_Sap`.`PP_Colors` LIMIT 1);
ELSE SET finalcolor = (SELECT `PP_Colors`.`S_NotApplicable` FROM `SCR_Sap`.`PP_Colors` LIMIT 1);
END IF;
END IF;
END IF;
END IF;
RETURN finalcolor;
END$$
DELIMITER ;
Null doesn't work well with = and != operators, the result of foobar=NULL is NULL (unknown) not true or false. Use IS NULL and IS NOT NULL instead