I Have a table somewhat like this:
rnumber
number = int, cod = int
SELECT * FROM number WHERE number = 21377 and cod = 55;
returns the correct value;
So i have a proc call to insert:
CREATE DEFINER=`root`#`%` PROCEDURE `create`(IN Numb INT, IN Cod INT, IN qt INT)
BEGIN
SET #Cont = 0;
SET #Init = Numb;
WHILE #Cont < qt DO
SET #Exist = (SELECT count(number) FROM rnumber WHERE number = #Init AND cod = Cod LIMIT 1);
IF #Exist = 0 THEN
INSERT INTO (...)
END IF;
SET #Cont = #Cont + 1;
SET #IniT = #Init + 1;
END WHILE;
END$$
CALL create (21377, 54, 1);
Always give me variable #Exist as 1, so no go on the if, even tho the combination of number and cod does not exists.
Can anyone point me what am i doing wrong?
Thank you.
Try using select into instead of set xx = (select...).
Also having cod = Cod will always return true...
You also need to declare your variables...
Try this one:
CREATE DEFINER=`root`#`%` PROCEDURE `create`(IN p_Numb INT, IN p_Cod INT, IN p_qt INT)
BEGIN
declare v_Exist integer;
declare v_Init integer;
declare v_Exist integer;
SET v_Cont = 0;
SET v_Exist = 0;
SET v_Init = p_Numb;
WHILE v_Cont < p_qt DO
SELECT count(number) into v_Exist FROM rnumber WHERE number = v_Init AND cod = p_Cod LIMIT 1;
IF v_Exist = 0 THEN
INSERT INTO (...)
END IF;
SET v_Cont = v_Cont +1;
SET v_IniT = v_Init + 1;
END WHILE;
END$$
Related
My approach
DELIMITER $$
CREATE FUNCTION fibonacci(num INT)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE fib1 INT DEFAULT 0;
DECLARE fib2 INT DEFAULT 1;
DECLARE fib3 INT DEFAULT 0;
DECLARE str VARCHAR(255) DEFAULT '01';
IF num = 1 THEN
RETURN fib1;
ELSEIF num = 2 THEN
RETURN CONCAT(fib1, fib2);
ELSE
WHILE num > 2 DO
SET fib3 = fib1 + fib2;
SET fib1 = fib2;
SET fib2 = fib3;
SET num = num - 1;
SET str = CONCAT(str, fib3);
END WHILE;
RETURN str;
END IF;
END $$
DELIMITER ;
If I call above function using SELECT fibonacci(6); it returns 11235 without leading zero(0). How I can show leading zero also?
i think it will help you
DELIMITER $$
CREATE FUNCTION fibonacci_number(n INT) RETURNS INT
DETERMINISTIC
BEGIN
DECLARE f_0 INT default 0;
DECLARE f_1 INT DEFAULT 1;
DECLARE out_fib INT;
DECLARE i INT;
DECLARE f_2 INT;
SET f_0 = 0;
SET f_1 = 1;
SET i = 1;
WHILE (i<=n) DO
SET f_2 = f_0 + f_1;
SET f_0 = f_1;
SET f_1 = f_2;
SET i = i + 1;
END WHILE;
SET out_fib = f_0;
RETURN out_fib;
END $$
I want to call a stored procedure in another one but the syntax doesn't seem to be correct.
I've tried to just skip the process and do the select command directly but that doesn't seem to work and besides I'd like to know how to do this.
DELIMITER //
create procedure Find_Eid(Pid int)
select EId from Employees inner join Patient on EId = EId_fk where Patient.PId = Pid;
//
DELIMITER ;
call Find_Eid(4);
drop procedure Fill_Interact;
DELIMITER //
create procedure Fill_Interact()
begin
declare N1 int;
declare TOT date;
declare Rid int;
declare Pid int;
declare Eid int;
set N1 = (select count(*) from Patient);
set TOT = curdate();
set Rid = 1;
set Pid = 1;
while N1 > 0 do
set Eid = (call Find_Eid(Pid));
insert into Interact
(Time_Of_Treatment, RId_fk, PId_fk, EId_fk)
values
(TOT,Rid,Pid, Eid);
if Rid = (select count(*) from Room limit 1) then
set Rid = 1;
set TOT = TOT + 1;
else
set Rid = Rid + 1;
end if;
set N1 = N1 - 1;
set Pid = Pid + 1;
end while;
end;
//
DELIMITER ;
call Fill_Interact();
select * from Interact;
If you want to assign the procedures value to another variable, You must use an out parameter in the proc. So your first proc should be -
DELIMITER //
create procedure Find_Eid(in Pid int, out eid int)
select EId into eid
from Employees
inner join Patient on EId = EId_fk
where Patient.PId = Pid;
//
Then you can use this proc inside your second procedure -
drop procedure Fill_Interact;
DELIMITER //
create procedure Fill_Interact()
begin
declare N1 int;
declare TOT date;
declare Rid int;
declare Pid int;
declare Eid int;
set N1 = (select count(*) from Patient);
set TOT = curdate();
set Rid = 1;
set Pid = 1;
while N1 > 0 do
call Find_Eid(Pid, Eid);
insert into Interact
(Time_Of_Treatment, RId_fk, PId_fk, EId_fk)
values
(TOT,Rid,Pid, Eid);
if Rid = (select count(*) from Room) then
set Rid = 1;
set TOT = TOT + 1;
else
set Rid = Rid + 1;
end if;
set N1 = N1 - 1;
set Pid = Pid + 1;
end while;
end;
//
DELIMITER ;
call Fill_Interact();
I have a database table which as below
I want to add each SoldD column to next row ValT column.
for example, query result for above given table should be like as
Above snapshot is for expected Results
so for, I have tried many things but failed to get expected results
Failed try 1:
SELECT *,(IFNULL((SELECT VaIT from saleorder s WHERE s.id > ss.id limit 1),0)+ss.SoldD) FROM `saleorder` ss
Failed try 2:
update saleorder SET SoldD=(IFNULL((SELECT VaIT from (SELECT * from saleorder WHERE id > id+1 limit 1) s),0)+SoldD)
Any help is highly appreciated, Thanks in advance!
MySql version:
CREATE DEFINER=`root`#`localhost` PROCEDURE `new_procedure`()
BEGIN
DECLARE id2 INT DEFAULT 0;
declare VALT2 INT DEFAULT 0;
declare soldD2 INT DEFAULT 0;
declare beforSo2 INT DEFAULT 0;
DECLARE cur1 CURSOR FOR SELECT Id, ValT, SoldD FROM saleorder ORDER BY id;
OPEN cur1;
FETCH cur1 INTO id2, VALT2, soldD2;
SET valT2 = soldD2;
x: LOOP
IF(valT2 IS NULL) THEN
SET valT2 = 0;
END IF;
IF(beforSo2 IS NULL) THEN
SET beforSo2 = 0;
END IF;
UPDATE saleorder
SET SoldD = beforSo2 + valT2
WHERE ID = id2;
SET beforSo2 = beforSo2 + valT2;
FETCH cur1 INTO id2, VALT2, soldD2;
END LOOP;
CLOSE cur1;
END
Then call procedure this way:
CALL new_procedure;
SqlServer version:
DECLARE db_cursor CURSOR FOR SELECT ID, ValT, SoldD FROM Table_2 ORDER BY ID;
DECLARE #ID INT;
DECLARE #valT INT;
DECLARE #soldD INT;
DECLARE #beforSoldD INT = 0;
OPEN db_cursor;
FETCH NEXT FROM db_cursor INTO #ID, #valT, #soldD;
SET #valT = #soldD;
WHILE ##FETCH_STATUS = 0
BEGIN
IF(#valT IS NULL)
SET #valT = 0;
IF(#beforSoldD IS NULL)
SET #beforSoldD = 0;
UPDATE Table_2
SET SoldD = #beforSoldD + #valT
WHERE ID = #ID;
SET #beforSoldD = #beforSoldD + #valT;
FETCH NEXT FROM db_cursor INTO #ID, #valT, #soldD;
END;
CLOSE db_cursor;
DEALLOCATE db_cursor;
Base data:
Result:
I need to convert this tSQL query into MySQL query version 5.7.14
I tried declare parameter with the following syntax
declare #commissionType INT;
but I reached a dead end and need help to convert this query
CREATE PROCEDURE MyProc
#chainId int
as
BEGIN
declare #commissionMethod int
declare #commissionType int
declare #value decimal(18,2)
set #commissionMethod = 1
set #value = 200000
set #commissionType = (select CommissionType from CommissionRules
where ChainId = #chainId )
IF(#commissionMethod = 1)
BEGIN
print('alert')
select (Fixed * #value) / 100 from CommissionRules where ChainId = #chainId
END
END
This is pretty much a literal conversion. But check your logic. It doesn't make any sense. v_commissionMethod is always 1 and v_commissionType is not even being used after selecting a value.
DELIMITER //
CREATE PROCEDURE MyProc (
p_chainId int)
BEGIN
declare v_commissionMethod int;
declare v_commissionType int;
declare v_value decimal(18,2);
set v_commissionMethod = 1;
set v_value = 200000;
select CommissionType into v_commissionType from CommissionRules
where ChainId = p_chainId ;
IF(v_commissionMethod = 1)
THEN
/* print('alert') */
select (Fixed * v_value) / 100 from CommissionRules where ChainId = p_chainId;
END IF;
END;
//
DELIMITER ;
Something like this:
DELIMITER $$
CREATE PROCEDURE MyProc (
in_chainId unsigned
) as
BEGIN
select v_CommissionType := CommissionType,
v_commissionMethod = 1,
v_value = 200000
from CommissionRules
where ChainId = in_chainId;
if v_commissionMethod = 1 then
select 'alert';
select (Fixed * v_value) / 100
from CommissionRules
where ChainId = in_chainId
end if;
END;$$
DELIMITER ;
I receive error 1318 when I call this procedure in MYSQL. What am I doing wrong with this stored procedure? Am I even allowed to do something like this?
CREATE DEFINER=`root`#`localhost` PROCEDURE `CN_renumber`
(
OUT #maxCn,
OUT param1 INT,
OUT update_count INT
)
BEGIN
DECLARE sql_error TINYINT DEFAULT FALSE;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET sql_error = TRUE;
START TRANSACTION;
/*Renumber Cn in DBO*/
/* Set max cn and incremented afterwords */
SET #maxCn = '23206';
UPDATE dbo.billadr SET Cn = (#maxCn:=#maxCn + 1);
IF sql_error = FALSE THEN SET update_count = 1;
COMMIT;
ELSE
SET update_count = 0;
ROLLBACK;
END IF;
END
There was a problem with parameters. Try this procedure, it will fix error 1318 -
CREATE DEFINER='root'#'localhost' PROCEDURE CN_renumber
(OUT maxCn int, OUT param1 int, OUT update_count int)
BEGIN
SET #maxCn = 23206;
UPDATE billadr SET Cn = (#maxCn := #maxCn + 1);
SET maxCn = #maxCn;
END
Usage example -
SET #maxCn = NULL;
SET #param1 = NULL;
SET #update_count = NULL;
CALL CN_renumber(#maxCn, #param1, #update_count);
SELECT #maxCn, #param1, #update_count;