i have a table with one column id and this column have (1,2,3,5,8,12,15,20)
i have find values that not in table WHIT CURSOR
this is a practice for Cursor
my query had error
please help me
declare cur cursor for select id from T7
open cur
declare #id int
declare #show1 int
declare #show2 int
declare #temp table (number int)
fetch next from cur into #id
while ##FETCH_STATUS = 0
begin
set #show1 = #id
fetch next from cur into #id
set #show2 = #id
if #show2 - #show1 = 1
begin
print 'true'
end
while #show2 - #show1 !=1
begin
set #show2 = #show2 - 1
insert into #temp select #show2
end
end
select * from #temp order by number asc
close cur
Thanks for taking
I do not have the numbers in this table
example values (4,6,7,9,10,11,13,14,16,17,18,19) in table temp
Related
Hello I have the following stored procedure for MySQL but when it is executed in my ASP.NET Core application I get a Subquery returns more than 1 row error. What am I doing wrong here? The equivalent SQL Server version used to work without problems...
-- System Calculates Candidate’s Matching Score based on a Manager’s Answer Weights
CREATE PROCEDURE spSysCalcCandScore
(
IN Candidate_ID INT,
IN Manager_ID INT
)
Begin
DECLARE ansID INT;
DECLARE tempSum INT;
DECLARE Sum INT;
DECLARE Done INT DEFAULT FALSE;
DECLARE MyCursor CURSOR FOR
SELECT Answer_ID FROM Completed_Questionnaire
WHERE Candidate_ID = Candidate_ID;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET Done = TRUE;
START TRANSACTION;
OPEN MyCursor;
myloop: LOOP
FETCH MyCursor INTO ansID;
IF Done THEN
LEAVE myloop;
END IF;
SET tempSum = (SELECT Weight_Value FROM Weight WHERE (Answer_ID = ansID AND Manager_ID = Manager_ID));
SET Sum = Sum + tempSum;
END LOOP;
CLOSE MyCursor;
IF (Sum IS NULL) THEN
SET Sum = 0;
END IF;
UPDATE `Interest`
SET Matching_Score = Sum
WHERE (Candidate_ID = Candidate_ID AND Manager_ID = Manager_ID);
COMMIT;
End//
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 have a predefined list of items which I want to use in select query.
DECLARE #cur_emp CURSOR;
DECLARE #TEST_ID int
DECLARE #TEST_NAME varchar(100)
DECLARE #names table(name varchar(100))
--SET #TEST_NAME = 'Order not timely approved'
insert into #names(name) values ('a')
insert into #names(name) values ('b')
--SET #TEST_NAME = CURSOR FOR (select name from #names)
WHILE EXISTS (select * from #names)
BEGIN
SET #cur_emp= CURSOR FOR (select TEST_ID from CS_TEST_V2 where test_name = #name)
Error says: #name is an invalid column name
Can somebody please help.
You have syntax errors in your cursor part. Please try this.
DECLARE #TEST_ID INT
DECLARE #TEST_NAME VARCHAR(100)
DECLARE #names TABLE (NAME VARCHAR(100))
--SET #TEST_NAME = 'Order not timely approved'
INSERT INTO #names (NAME)
VALUES ('a')
INSERT INTO #names (NAME)
VALUES ('b')
DECLARE cur_emp CURSOR
FOR
SELECT NAME
FROM #names;
OPEN cur_emp
FETCH NEXT FROM cur_emp
INTO #TEST_NAME
WHILE ##FETCH_STATUS = 0
BEGIN
SET #TEST_ID = (
SELECT TEST_ID
FROM CS_TEST_V2
WHERE test_name = #TEST_NAME
)
FETCH NEXT FROM cur_emp
INTO #TEST_NAME
END
CLOSE cur_emp
DEALLOCATE cur_emp
Can anyone shed light on why this will not loop through the function and populate the temp table? Ive tried a number of things and at best can only get the first value to populate. I'm trying to take a value and a ":" separated string (queried in this procedure) to populate a table so I can reference it in a larger query. The function SPLIT_STR works great on its own but I cant seem to increment value "a" so that it separates ALL values per field per value.
BEGIN
DECLARE platform_val VARCHAR(255);
DECLARE productName_val VARCHAR(255);
DECLARE no_more_rows BOOLEAN;
DECLARE num_rows INT DEFAULT 0;
DECLARE loop_cntr INT DEFAULT 0;
DECLARE str VARCHAR(255);
DECLARE a INT DEFAULT 1;
DECLARE cur1 CURSOR FOR SELECT
ProductName,
ProductPlatforms
FROM Feed
limit 10;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_rows = TRUE;
OPEN cur1;
select FOUND_ROWS() into num_rows;
the_loop: LOOP
FETCH cur1
INTO productName_val,
platform_val;
SET str = platform_val;
WHILE a< num_rows DO
SET str=SPLIT_STR(str,":",a);
insert into temp values (productName_val, str);
SET a=a+1;
END WHILE;
END LOOP the_loop;
END
This is the idea I came up with
CREATE PROCEDURE `col2lines`()
BEGIN
declare v_platform,v_productName,v_platformAll varchar(255) default null;
declare v_finished int default 0;
declare curs cursor for select ProductName,ProductPlatforms from Feed limit 10;
declare continue handler for not found set v_finished = 1;
drop temporary table if exists temp_table;
create temporary table temp_table (
productName varchar(255),
platform varchar(255)
);
open curs;
parent: LOOP
fetch curs into v_productName, v_platformAll;
if (v_finished = 1) then LEAVE parent; end if;
child: LOOP
set v_platform = substring_index(v_platformAll, '::', 1);
set #strlen = char_length(v_platform);
if (#strlen > 0) then
insert into temp_table values (v_productName, v_platform);
set v_platformAll = substr(v_platformAll, #strlen + 3);
iterate child;
end if;
LEAVE child;
END LOOP child;
iterate parent;
END LOOP parent;
close curs;
select * from temp_table;
END
I have been debugging a SQL stored procedure which has to take values (in my code ID and Numb) form table A based on the values (ID) present in the Table C, then square the Numb and store it in Table B i.e. all the things ID, Numb and Square.
I am not able to figure out the problem in the below code
DELIMITER $$
CREATE PROCEDURE matlab.squaring
BEGIN
DECLARE finish BOOLEAN DEFAULT 0; # <- set up initial conditions
DECLARE square BIGINT(10);
DECLARE ID INT(10);
DECLARE Numb INT (10);
DECLARE id_cur CURSOR FOR
SELECT ID, Numb FROM A WHERE EXISTS ( SELECT ID FROM c);
SET #square= #Numb * #Numb
INSERT INTO B
(
ID ,
Numb ,
square
) values ( ID , Numb, square);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finish = TRUE;
OPEN id_cur;
the_loop : LOOP
FETCH id_cur INTO ID;
IF finish THEN
CLOSE id_cur;
LEAVE the_loop;
END IF
END LOOP the_loop;
END$$
When I run the stored procedure the error that pops up is "there seems to be some syntax error in your code, please refer to MYSql guide. "
edit:
one more help please how to execute this stored procedure.
There are various minor errors;
You need a parameter list, even if empty for the procedure;
CREATE PROCEDURE matlab.squaring()
The continue handler needs to be right below the other declarations;
DECLARE id_cur CURSOR FOR
SELECT ID, Numb FROM A WHERE EXISTS ( SELECT ID FROM c);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET #finish = TRUE;
You forgot a semicolon;
SET #square= #Numb * #Numb;
You forgot # on the variable usages;
) values ( #ID , #Numb, #square);
You forgot a semicolon on END IF
END IF;
Just as an overview, here's the complete thing updated;
CREATE PROCEDURE matlab.squaring()
BEGIN
DECLARE finish BOOLEAN DEFAULT 0; # <- set up initial conditions
DECLARE square BIGINT(10);
DECLARE ID INT(10);
DECLARE Numb INT (10);
DECLARE id_cur CURSOR FOR
SELECT ID, Numb FROM A WHERE EXISTS ( SELECT ID FROM c);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET #finish = TRUE;
SET #square= #Numb * #Numb;
INSERT INTO B
(
ID ,
Numb ,
square
) values ( #ID , #Numb, #square);
OPEN id_cur;
the_loop : LOOP
FETCH id_cur INTO ID;
IF finish THEN
CLOSE id_cur;
LEAVE the_loop;
END IF;
END LOOP the_loop;
END//
You have missed () after PROCEDURE matlab...
And ; after END IF
Also, HANDLER declaration should be before any executable code and
after CURSOR declaration
Semicolon after SET #square= #Numb * #Numb is needed
So, query should be like this:
DELIMITER $$
CREATE PROCEDURE matlab.squaring ()
BEGIN
DECLARE finish BOOLEAN DEFAULT 0; # <- set up initial conditions
DECLARE square BIGINT(10);
DECLARE ID INT(10);
DECLARE Numb INT (10);
DECLARE id_cur CURSOR FOR
SELECT ID, Numb FROM A WHERE EXISTS ( SELECT ID FROM c);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finish = TRUE;
SET #square= #Numb * #Numb;
INSERT INTO B
(
ID ,
Numb ,
square
) values ( ID , Numb, square);
OPEN id_cur;
the_loop : LOOP
FETCH id_cur INTO ID;
IF finish THEN
CLOSE id_cur;
LEAVE the_loop;
END IF;
END LOOP the_loop;
END$$
Missed parameter brackets and semicolon at endif.
DELIMITER $$
CREATE PROCEDURE squaring()
BEGIN
DECLARE finish BOOLEAN DEFAULT 0; # <- set up initial conditions
DECLARE square BIGINT(10);
DECLARE ID INT(10);
DECLARE Numb INT (10);
DECLARE id_cur CURSOR FOR
SELECT ID, Numb FROM A WHERE EXISTS ( SELECT ID FROM c);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finish = TRUE;
SET #square= #Numb * #Numb;
INSERT INTO B
(
ID ,
Numb ,
square
) values ( ID , Numb, square);
OPEN id_cur;
the_loop : LOOP
FETCH id_cur INTO ID;
IF finish THEN
CLOSE id_cur;
LEAVE the_loop;
END IF;
END LOOP the_loop;
END$$