Running this routine on HeidiSQL and it runs with 0 rows affected, even though there should be exactly 1 row affected. both select statements seem to work fine outside this Stored Procedure.
BEGIN
DECLARE someId INT;
DECLARE done INT DEFAULT FALSE;
DECLARE cur1 CURSOR FOR
select anotherId from tableA
where yetAnotherId IN(another select statement);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
IF done THEN
LEAVE read_loop;
END IF;
FETCH cur1 INTO someId;
update tableB
set x = 'hello', y = 'world'
where something = someId;
END LOOP;
CLOSE cur1;
END;
I would like to get an idea what could be wrong with the structure of this routine. It looks to me that even thoughthe cursor should contain 1 entry, It does not.
Thanks
EDIT: It looks like 'someId' was matching a table field with the same name, thus the issue. This has been resolved now.
Your SP looks good, I have made changes for MySql please try it
BEGIN
DECLARE someId INT;
DECLARE done INT DEFAULT FALSE;
DECLARE cur1 CURSOR FOR
select anotherId from tableA
where yetAnotherId IN(another select statement);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO someId;
update tableB
set x = 'hello', y = 'world'
where something = someId;
IF done THEN
CLOSE cur1;
LEAVE read_loop;
END IF;
END LOOP read_loop;
END;
CREATE PROCEDURE curLike()
BEGIN
DECLARE likeRec likecounttable;
DECLARE c_likeCount CURSOR FOR SELECT l.likeCount, l.qId FROM likecounttable l;
OPEN c_likeCount;
start_loop:LOOP
FETCH c_likeCount IN likeRec
UPDATE qentry SET qentry.likeCount = likeRec.likeCount WHERE qentry.qId=likeRec.qId;
END LOOP;
CLOSE c_likeCount;
END;
I am trying to use a cursor here which fetches records from likecounttable, I saw this type of syntax in few sites so I used it but it is not working
You are missing a semi-colon after your first declaration, furthermore, likecounttable is a table, not a data type.
Since you're trying to store two column values into your declared variables, your first line should look more like this
DECLARE likeRec_Count, likeRec_qId INT;
After reading your code, if you aren't adding to your cursor, you can simplify by using the following sql instead, which does the same thing as your cursor.
UPDATE qentry JOIN likecounttable l ON l.qId=qentry.qId
SET qentry.likeCount = l.likeCount
;
EDIT: If you wanted a complete update to your cursor, the following should do the same thing.
DELIMITER $$
CREATE PROCEDURE curLike()
BEGIN
DECLARE c_likeRec_isdone BOOLEAN DEFAULT FALSE;
DECLARE likeRec_Count, likeRec_qId INT;
DECLARE c_likeCount CURSOR FOR SELECT l.likeCount, l.qId FROM likecounttable l;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET c_likeRec_isdone = TRUE;
OPEN c_likeCount;
loop_likeRecList: LOOP
FETCH c_likeCount INTO likeRec_Count, likeRec_qId;
IF c_likeRec_isdone THEN
SET c_likeRec_isdone = FALSE;
LEAVE loop_likeRecList;
END IF;
UPDATE qentry SET qentry.likeCount = likeRec_Count WHERE qentry.qId=likeRec_qId;
END LOOP loop_likeRecList;
CLOSE c_likeCount;
END;
$$
CREATE PROCEDURE curLike()
BEGIN
DECLARE likeRec_Count, likeRec_qId INT;
DECLARE c_likeCount CURSOR FOR SELECT l.likeCount, l.qId FROM likecounttable l;
OPEN c_likeCount;
start_loop:LOOP
FETCH c_likeCount INTO likeRec_Count,likeRec_qId
UPDATE qentry SET qentry.likeCount = likeRec_Count WHERE qentry.qId=likeRec_qId ;
END LOOP;
CLOSE c_likeCount;
END;
I'm unable to get the result of OUT variable in this code, am I doing wrong something? can anyone help?
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE cid INT(10);
DECLARE cuserid VARCHAR(50);
DECLARE cur1 CURSOR FOR SELECT id,username FROM tblcustomer;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO cid,cuserid;
IF done THEN
LEAVE read_loop;
END IF;
SET customers_list = CONCAT(customers_list,cid,':',cuserid,',');
END LOOP;
CLOSE cur1;
END
The procedure prototype is missing from your snippet, but assuming the below:
CREATE PROCEDURE foo(OUT customers_list VARCHAR(100))
BEGIN
...
SET customers_list = 'foo-list';
END ;
This is how you would retreive your "return value":
CALL foo(#var);
SELECT #var; -- outputs "foo-list"
Strictly speaking, a procedure has no "return value". A function has:
CREATE FUNCTION bar() RETURNS VARCHAR(100)
BEGIN
...
RETURN 'bar-list';
END ;
SELECT bar(); -- outputs "bar-list";
I have create trigger for some condition but its through error.
Here is trigger code.
DELIMITER //
CREATE TRIGGER `getcrm`.`update_round_date` AFTER UPDATE ON `getcrm`.`vtiger_stockcheckcf`
FOR EACH ROW begin
DECLARE retribAn INTEGER DEFAULT 0;
DECLARE curEdate datetime;
DECLARE done INT DEFAULT 0;
DECLARE curTipo CURSOR FOR
SELECT a.*, b.*, c.* FROM vtiger_crmentity AS a, vtiger_stockcheck AS b,vtiger_stockcheckcf AS c WHERE a.crmid = b.stockcheckid AND a.crmid = c.stockcheckid AND c.cf_746 = 'Pending' AND a.setype='StockCheck';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
OPEN curTipo;
REPEAT
FETCH curTipo INTO retribAn;
IF NOT done THEN
set curEdate = NOW();
IF a.createdtime = curEdate AND c.cf_746 = Pending THEN
INSERT INTO vtiger_cancellationcf(scheckid,cf_1150,cf_1152,cf_1154,cf_1156,cf_1151,cf_1153,cf_1155,cf_1157,cf_1158,cf_1160,cf_1162,cf_1159,
cf_1161,cf_1163,cf_1203,cf_1178,cf_1180,cf_1182,cf_1184,cf_1186,cf_1178,cf_1180,cf_1182,cf_1184,cf_1186,cf_1179,cf_1181,
cf_1183,cf_1185,cf_1187,cf_1179,cf_1181,cf_1183,cf_1185,cf_1187,cf_1164,cf_1166,cf_1168,cf_1170,cf_1165,cf_1167,cf_1169,
cf_1171,cf_1173,cf_1176,cf_1177,cf_1172,cf_1174,cf_1175,cf_1188,cf_1189,cf_1190) values ('a.crmid','"c.cf_709"','"c.cf_711"','"c.cf_713"','"c.cf_715"','"c.cf_710"','"c.cf_712"','"c.cf_714"',
'"c.cf_716"','"c.cf_717"','"c.cf_719"','"c.cf_721"','"c.cf_718"','"c.cf_720"','"c.cf_844"','"c.cf_1079"','"c.cf_736"',
'"c.cf_738"','"c.cf_740"','"c.cf_742"','"c.cf_744"','"c.cf_737"','"c.cf_739"','"c.cf_741"','"c.cf_743"','"c.cf_745"',
'"c.cf_722"','"c.cf_724"','"c.cf_726"','"c.cf_723"','"c.cf_725"','"c.cf_727"','"c.cf_729"','"c.cf_731"','"c.cf_734"',
'"c.cf_735"','"c.cf_730"','"c.cf_732"','"c.cf_733"','"b.stockcheck"','"c.cf_746"','"c.cf_1080"','"c.cf_1081"');
insert into vtiger_cancellation (cancellationid,cancellation,scheckid) values ('a.crmid',' ','c.cf_709');
insert into vtiger_crmentity (crmid,smcreatorid,smownerid,modifiedby,setype,description,createdtime,modifiedtime,viewedtime,status,version,presence,deleted,label) values ('a.crmid','a.smcreatorid','a.smownerid','a.smcreatorid','Cancellation','','curEdate','curEdate','','','0','1','0','');
delete FROM vtiger_crmentity_seq where id='a.crmid';
insert into vtiger_crmentity_seq (id)values ('a.crmid');
END IF;
END IF;
UNTIL done END REPEAT;
CLOSE curTipo;
end
//
DELIMITER
Here is error when any changes on table.
1328 - Incorrect number of FETCH variables
Thanks for advance
I think the problem is here : FETCH curTipo INTO retribAn; your trying to fetch a complete row into a integer.
You should read more on CURSORS .
EXAMPLE:
DECLARE a CHAR(16);
DECLARE b INT;
DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;
OPEN cur1;
FETCH cur1 INTO a, b;