delimiter $$
create procedure mergeData()
begin
declare counter, rowws int default 0;
set rowws = (select count(*) from new_branches);
begin
while counter < rowws do
if new_branches.branch_no != branch_mstr.branch_no then
insert into branch_mstr value(new_branches.branch_no, new_branches.name);
end while;
end;
end$$
delimiter ;
Why these crosses are showing, I am using MySQL and new to the PL/SQL. Asking for the help
I created a simple stored procedure that loops through rows of one table and inserts them into another. For some reason the END WHILE loop is throwing a missing semicolon error. All the code looked right to me, and all the delimiters were set up right. I just can't figure out why it would be throwing these errors, googling this problem only pointed me to improperly used delimiter answers, but nothing more. Any help would be nice!
USE test;
DELIMITER $$
DROP PROCEDURE IF EXISTS `testLoop`$$
CREATE PROCEDURE `testLoop`()
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
SELECT COUNT(*) FROM `test_dropship_upload` INTO n;
SET i=0;
WHILE i<n DO
INSERT INTO `test_2` (sku, qty) VALUES(sku, qty) FROM `test_dropship_upload` LIMIT i,1;
SET i = i + 1;
END WHILE;
END $$
DELIMITER ;
When I am stuck with a problem in a large block of code and can't find where the problem is, I usually split my code in smaller chunks and test them one at a time:
Test 1:
DELIMITER $$
DROP PROCEDURE IF EXISTS `testLoop`$$
CREATE PROCEDURE testLoop()
BEGIN
END $$
DELIMITER ;
No errors: procedure declaration and use of delimiters is OK.
Test 2:
DELIMITER $$
DROP PROCEDURE IF EXISTS `testLoop`$$
CREATE PROCEDURE `testLoop`()
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
END $$
DELIMITER ;
No errors: the declaration of variables within the procedure is OK.
Test 3:
DELIMITER $$
DROP PROCEDURE IF EXISTS `testLoop`$$
CREATE PROCEDURE `testLoop`()
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
SELECT COUNT(*) FROM `test_dropship_upload` INTO n;
SET i=0;
END $$
DELIMITER ;
No errors: the SELECT query and the variable assignment are OK.
Test 4:
DELIMITER $$
DROP PROCEDURE IF EXISTS `testLoop`$$
CREATE PROCEDURE `testLoop`()
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
SELECT COUNT(*) FROM `test_dropship_upload` INTO n;
SET i=0;
WHILE i<n DO
SET i = i + 1;
END WHILE;
END $$
DELIMITER ;
No errors: the WHILE loop is OK.
Test 5:
The only untested part is now the INSERT query:
INSERT INTO `test_2` (sku, qty) VALUES(sku, qty) FROM `test_dropship_upload` LIMIT i,1;
Looking a the documentation for INSERT and INSERT ... SELECT, we can see that your query is not valid: it is apparently missing a SELECT part and shouldn't have a VALUES part if you want to insert values from another table:
DELIMITER $$
DROP PROCEDURE IF EXISTS `testLoop`$$
CREATE PROCEDURE `testLoop`()
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
SELECT COUNT(*) FROM `test_dropship_upload` INTO n;
SET i=0;
WHILE i<n DO
INSERT INTO `test_2` (sku, qty) SELECT sku, qty FROM `test_dropship_upload` LIMIT i, 1;
SET i = i + 1;
END WHILE;
END $$
DELIMITER ;
The procedure creation now completes without errors.
Test 6:
However, you will get a syntax error on the SELECT query when executing the procedure: MySQL doesn't accept using LIMIT with a variable.
To make it work, you need to use a prepared statement.
PREPARE stmt FROM "INSERT INTO `test_2` (sku, qty) SELECT sku, qty FROM `test_dropship_upload` LIMIT ?, 1";
EXECUTE stmt using #i;
DEALLOCATE PREPARE stmt;
It is also not allowed to used local variables in prepared statements:
Because local variables are in scope only during stored program
execution, references to them are not permitted in prepared statements
created within a stored program. Prepared statement scope is the
current session, not the stored program, so the statement could be
executed after the program ends, at which point the variables would no
longer be in scope. For example, SELECT ... INTO local_var cannot be
used as a prepared statement. This restriction also applies to stored
procedure and function parameters.
To circumvent this problem, use a session variable #i instead of your local variable i:
Final version of the procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS `testLoop`$$
CREATE PROCEDURE `testLoop`()
BEGIN
DECLARE n INT DEFAULT 0;
SELECT COUNT(*) FROM `test_dropship_upload` INTO n;
SET #i=0;
WHILE #i<n DO
PREPARE stmt FROM "INSERT INTO `test_2`(sku, qty) SELECT sku, qty FROM `test_dropship_upload` LIMIT ?, 1";
EXECUTE stmt USING #i;
DEALLOCATE PREPARE stmt;
SET #i = #i + 1;
END WHILE;
END $$
DELIMITER ;
You can apply the same method to debug many complex programming problems: start with a simple version of your code, test it. If it works test again with a more code, if not locate and fix the errors before continuing.
I am using a procedure to loop through a table and perform a different procedure on each row (this procedure uses other tables and works fine, returns correct result set). Here is my attempt:
DELIMITER $$
CREATE PROCEDURE user_loop()
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
SELECT COUNT(*) FROM users INTO n;
SET i=0;
WHILE i<n DO
SET #cur_id = (SELECT user_id from users LIMIT i,1);
INSERT INTO results CALL other_proc(#cur_id);
SET i = i + 1;
END WHILE;
End;
$$
Not sure what the correct syntax is in order to do this, I looked but couldn't find any examples.
How does one check if a select statement inside a stored procedure returns any rows.
select * from creditcards;
If sqlcod = 0 THEN
I'd like to do something like this for example but sqlcod doesn't seem to work in MySql.
try using COUNT,
DELIMITER $$
CREATE PROCEDURE procName()
BEGIN
SET #recCount = (select count(*) from creditcards);
If #recCount = 0 THEN
-- statement here ;
END IF;
END $$
DELIMITER ;
Try using like below also:
Use FOUND_ROWS() and SQL_CALC_FOUND_ROWS:
DELIMITER $$
create procedure myproc()
begin
SELECT SQL_CALC_FOUND_ROWS *
FROM tbl_name
WHERE id > 100
LIMIT 10;
if SELECT FOUND_ROWS() = 0 then
-- log away bro!
end if;
end $$
DELIMITER $$
I am just starting to learn how to write stored procedures in MYSQL and I've hit a roadblock.
I wrote the following code:
DELIMITER $$
DROP PROCEDURE IF EXISTS `emscribedx`.`countcodes` $$
CREATE PROCEDURE `emscribedx`.`countcodes` ()
BEGIN
declare doneprocessing int default 0;
declare thisaccount varchar(50);
declare countcursor cursor for select acct from patientid where patienttype='P';
declare continue handler for not found
set doneprocessing = 1;
Fetch countcursor into thisaccount;
Repeat
select * from doc_table where acct = thisaccount;
until doneprocessing = 1
END repeat;
close countcursor;
END $$
DELIMITER ;
I would like to display the results of the select statement that occurs after the repeat statement. But how do I do that? When I execute the stored procedure, nothing happens?
Thank you,
Elliott
The procedure can only return the results of a single query, why not use this instead?
SELECT doc_table.*
FROM doc_table
INNER JOIN patientid
ON patientid.acct = doc_tableacct
WHERE patientid.patienttype='P';
It can be put inside of a procedure if you want.