How to call stored procedure at sqlfiddle? - mysql

I put the following into the Schema panel at sqlfiddle:
CREATE TABLE tb_patient (
`idPatient` INTEGER,
`prenomPatient` VARCHAR(12),
`nomPatient` VARCHAR(6)
)//
INSERT INTO tb_patient
(`idPatient`, `prenomPatient`, `nomPatient`)
VALUES
('267', 'Marie Claude', 'CARRIE'),
('268', 'Marie Claude', 'CARRIE')//
create procedure findTwins()
begin
declare getNom varchar(40);
declare getPrenom varchar(40);
declare getId int default 1;
declare getId2 int default 1;
if(select count(*) from tb_patient group by nomPatient,prenomPatient having count(*)=2 limit 1)
then
select nomPatient,prenomPatient into getNom,getPrenom from tb_patient group by nomPatient,prenomPatient having count(*)=2 limit 1;
set getId=(select min(idPatient) from tb_patient where nomPatient=getNom and prenomPatient=getPrenom);
set getId2=(select max(idPatient) from tb_patient where nomPatient=getNom and prenomPatient=getPrenom);
select concat(getNom,' ',getPrenom,' ',getId,' ',getId2) as Patient;
end if;
end//
I selected // from the delimiter menu, and successfully built the schema.
Then I put:
CALL FindTwins
in the query panel. When I tried to run the query, I got the error message:
DDL and DML statements are not allowed in the query panel for MySQL; only SELECT statements are allowed. Put DDL and DML in the schema panel.
How am I supposed to see the result of the procedure if I can't put a call in the
query panel?
http://www.sqlfiddle.com/#!9/b03ede/4

This is a SQLFiddle bug. From https://github.com/zzzprojects/sqlfiddle3/issues/5:
Unfortunately, I don't think this statement currently work in this
version.
The good news is that we are currently working on a new version. The
new version should allow this without a problem but unfortunately, we
need more time before releasing it.
This seems to work in earlier versions: Execute triggers stored procedures on SqlFiddle. Mysql.

Related

Stored Procedure ahowing 'lock wait timeout exceeded try restarting transaction in mysql stored procedure' error?How to handel it?

Hello guys i am trying to validate otp verification manually.for that i have written this stored procedure .When i started this code was working fine but after some time its start giving error.It was taking too long time to execute approx 50sec and after that it was giving the error lock wait timeout.So can anyone tell me why its giving such error and how to resolve it?
CREATE DEFINER=`xxxxx`#`xxxx` PROCEDURE `new_mobile_authentication`(
IN in_macID VARCHAR(500),IN in_otp INT(5),OUT in_msg VARCHAR(100))
BEGIN
DECLARE userCount INT(10);
DECLARE emailID VARCHAR(100);
DECLARE mobileNumber BIGINT(11);
DECLARE checkmatched INT(5);
DELETE FROM mob_user WHERE NOW()>end_time;
SELECT COUNT(*),email,mobile,otp into userCount,emailID,mobileNumber,checkmatched FROM mob_user WHERE mac_id=in_macID ;
SET #checkEmailPresent=(SELECT COUNT(*) FROM table A WHERE email_id=emailID);
IF(userCount!=0 AND #checkEmailPresent!=0)THEN
IF(checkmatched=in_otp)THEN
UPDATE table A SET auth='YES',mac_id=in_macID,mobile_num=mobileNumber WHERE email=emailID;
SET #affRow=(SELECT ROW_COUNT());
DELETE FROM mob_user WHERE mac_id=in_macID;
SELECT #affRow AS affRow,email FROM table A WHERE mac_id=in_macID;
ELSE
SELECT 'invalid otp' INTO in_msg;
END IF;
ELSEIF(userCount!=0 AND #checkEmailPresent=0)THEN
IF(checkmatched=in_otp)THEN
INSERT INTO table A(email,mobile_num,mac_id) VALUE (emailID,mobileNumber,in_macID,);
SET #affRow=(SELECT ROW_COUNT());
DELETE FROM mob_user WHERE mac_id=in_macID;
SELECT #affRow AS affRow,email FROM table A WHERE mac_id=in_macID;
ELSE
SELECT 'invalid otp' INTO in_msg;
END IF;
ELSE
SELECT 'session expired' INTO in_msg;
END IF;
END
Fix timeouts by adding indexes and/or reformulating queries.
mob_user needs INDEX(end_time) and INDEX(mac_id).
SELECT COUNT(*), this, that ... without a GROUP BY does not makes sense. Nor will it work right with a GROUP BY. What were you expecting??
SET #checkEmailPresent=(SELECT COUNT(*) FROM table A WHERE email_id=emailID) can be rewritten SELECT #checkEmailPresent := COUNT(*) FROM table A WHERE email_id=emailID). Note the :=. That table needs INDEX(email_id).
IF(checkmatched=in_otp)THEN does not make sense since checkmatched is nowhere set. No that SELECT does not set it.
What is ROW_COUNT()? I don't think it is a MySQL function.

can we create create statement inside cursor loop in oracle

Can we do CTAS inside a cursor in oracle
I am trying below code
declare
l_email_string varchar2(100);
cursor c1 is
select * from EMAIL_OBS where rownum < 2;
begin
for rec in C1
loop
create table ABC_TEST
(
row_id ,
email_string
)
as
select
rowid ,
jasbk
from EMAIL_OBS ;
end loop ;
end ;
/
but it is showing error while if I remove CTAS then it is working fine
Please suggest
Thanks ,
Abhimpi
You cannot perform DDL in PL/SQL like this (CTAS is DDL). You will need to use Dynamic SQL. Look up 'EXECUTE IMMEDIATE' for examples.

No-Data error in stored procedure

I am in the process of converting a SQL Server 2005 database to MySQL and having problems with a Stored procedure. I'm new to MySQL stored procedures so I'm sure it is a problem with my conversion but I'm not seeing it.
The stored procedure is supposed to generate a temporary table which is used to populate a Data Grid View in a vb.net application. However, I'm getting the error "Data No Data - Zero rows fetched, selected or processed.". Seems simple enough but the select procedure in the stored procedure will get data if I just run it as a query which is why I don't understand why the error.
I'm really hoping someone can tell me why because I have several hundred stored procedures to convert and I'm having this problem on the very first one.
Here's the Stored Procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS `usp_get_unassigned_media`$$
CREATE DEFINER=`showxx`#`67.111.11.110` PROCEDURE `usp_get_unassigned_media`()
BEGIN
/* GET CURSOR WITH LOCAL LOCATIONS */
DECLARE intKey INT;
DECLARE dteDateInserted DATETIME;
DECLARE vchIdField VARCHAR(200);
DECLARE vchValueField VARCHAR(200);
DECLARE intLastKey INT;
/*TAKE OUT SPECIFIC PLAYLIST ITEMS IF TOO SLOW*/
DECLARE csrMediaToBeAssigned CURSOR FOR
SELECT
`media`.`key` AS `key`,
`media`.`date_inserted` AS `date_inserted`,
`media_detail_types`.`id` AS `id`,
`media_details`.`value` AS `value`
FROM (`media`
LEFT JOIN (`media_detail_types`
JOIN `media_details`
ON ((`media_detail_types`.`key` = `media_details`.`detail_key`)))
ON ((`media_details`.`media_key` = `media`.`key`)))
WHERE ((`media`.`is_assigned` = 0)
AND ((`media_detail_types`.`id` = 'Volume Name')
OR (`media_detail_types`.`id` = 'Drive Id')))
ORDER BY `media`.`key`,`media`.`date_inserted`,`media_detail_types`.`id`;
OPEN csrMediaToBeAssigned;
DROP TEMPORARY TABLE IF EXISTS temp_unassigned_media;
CREATE TEMPORARY TABLE temp_unnassigned_media
(temp_key INT, DateInserted DATETIME, IdField VARCHAR(200), ValueField VARCHAR (200))
ENGINE=MEMORY;
SET intLastKey = 0;
/*--GET FIRST RECORD */
FETCH FROM csrMediaToBeAssigned
INTO intKey, dteDateInserted, vchIdField, vchValueField;
/*--LOOP THROUGH CURSOR */
WHILE intLastKey = 0 DO
/*--DATA SHOULD BE IN DRIVE ID THEN VOLUME NAME */
INSERT INTO temp_unnassigned_media
VALUES (intKey, dteDateInserted, vchValueField, '');
FETCH NEXT FROM csrMediaToBeAssigned
INTO intKey, dteDateInserted, vchIdField, vchValueField;
UPDATE temp_unnassigned_media
SET IdField = vchValueField
WHERE temp_key = temp_key;
FETCH NEXT FROM csrMediaToBeAssigned
INTO intKey, dteDateInserted, vchIdField, vchValueField;
END WHILE;
SELECT *
FROM temp_unnassigned_media
ORDER BY date_inserted;
CLOSE csrMediaToBeAssigned;
/*DEALLOCATE csrMediaToBeAssigned */
/*DROP TABLE #temp_unnassigned_media */
END$$
DELIMITER ;
You never hit a condition where that WHILE loop will exit; you initialize intLastKey variable, but it never changes, so you fetch through the entire resultset. The exception is thrown when you fetch again, after the last record.
The normative pattern is to declare a CONTINUE HANDLER, which MySQL will execute when the NOT FOUND condition is triggered. The handler is normally used to set a variable, which you can then test, so you know when to exit the loop.
In your case, it looks like just adding this line, after your DECLARE CURSOR statement and before the OPEN statement, would be sufficient:
DECLARE CONTINUE HANDLER FOR NOT FOUND SET intLastKey = 1;

Assigning the value from a select statement to a vairable in MySQL

I'm fairly new to SQL in general and even more so to MySQL and I've hit a stumbling block. I'm attempting to use a procedure to copy the value of one field to another if the original field is not null, this procedure is then called by triggers whenever the table is updated or has a new row inserted into it. Here is what I have so far:
-- WORK_NOTES_PROCEDURE - This copies the contents of the estimate notes to the work order notes if the original estimate had any notes with it.
DROP PROCEDURE IF EXISTS 'WORK_NOTES_PROCEDURE';
DELIMITER $$
CREATE PROCEDURE WORK_NOTES_PROCEDURE()
BEGIN
DECLARE var_temp VARCHAR(50);
SET var_temp := (SELECT ESTIMATE_NOTES FROM ESTIMATES WHERE ESTIMATES.ESTIMATE_NUMBER = WORK_ORDERS.ESTIMATE_NUMBER);
IF var_temp IS NOT NULL THEN
UPDATE WORK_ORDERS SET WORK_ORDER_NOTES = var_temp WHERE WORK_ORDERS.ESTIMATE NUMBER = ESTIMATES.ESTIMATE_NUMBER;
END IF;
END$$
DELIMITER ;
Absolutely any help would be appreciated, the error I'm getting is a syntax error for the line where I'm assigning a value to var_temp.
try,
SET var_temp = (SELECT ESTIMATE_NOTES
FROM ESTIMATES INNER JOIN WORK_ORDERS
ON ESTIMATES.ESTIMATE_NUMBER = WORK_ORDERS.ESTIMATE_NUMBER
LIMIT 1);

MYSQL Procedures run, but return 0 rows affected

I call a procedure, it runs, and the console says "0 rows affected". Is this normal behavior for a MySQL procedure ?
The procedures are clearly doing what they should. One procedure has 2 insert statements, another has an insert and update statement, and I've seen the results with my own eyes. There are indeed rows being affected.
I'm not sure that I would use that result later on, but it seems like I'd want to get an accurate response from my DB whether or not anything was updated, especially when its expected.
Thoughts ?
MySQL 5.5 if it matters, and the procedures use transactions over auto-committed statements.
CREATE DEFINER=`root`#`localhost` PROCEDURE `create_issue`(user_id SMALLINT, title varchar(255), body LONGTEXT)
BEGIN
DECLARE MYUSERID SMALLINT;
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN END;
START TRANSACTION;
INSERT INTO tracker.issue (user_id, title, body, creation_date, last_mod_date) values (user_id, title, body, CURDATE(), CURDATE());
UPDATE user_activity SET last_new_issue = CURDATE(), post_count = post_count + 1 WHERE user_activity.user_id = user_id;
COMMIT;
END
Edited to show the actual query. Also I've been searching and as best as I can tell this is a known issue over a year and a half old. So I suppose this one can be closed.
the "0 rows affected" response is for the last statement executed in the stored procedure.
usually i track the number of rows effected by manually counting them into session variables
DELIMITER $$
CREATE PROCEDURE `create_issue`(user_id SMALLINT, title varchar(255), body LONGTEXT)
BEGIN
DECLARE MYUSERID SMALLINT;
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN END;
SET #inserted_rows = 0;
SET #updated_rows = 0;
START TRANSACTION;
INSERT INTO tracker.issue (user_id, title, body, creation_date, last_mod_date) values (user_id, title, body, CURDATE(), CURDATE());
SET #inserted_rows = ROW_COUNT() + #inserted_rows;
UPDATE user_activity SET last_new_issue = CURDATE(), post_count = post_count + 1 WHERE user_activity.user_id = user_id;
SET #updated_rows = ROW_COUNT() + #updated_rows;
COMMIT;
END
$$
the session variables can then be read after the SP was executed.
i am not sure if it is possible to override the response from the ROW_COUNT() function by setting a value to a variable,
I guess this is a reported bug. May be a good question for MySQL mailing list/forum. http://bugs.mysql.com/bug.php?id=44854
Something definitely isn't right. A sproc should still return the number of rows affected if there are multiple inserts occurring. I'm using the same version of MySQL and this works fine for me.
Are you sure you're not doing something like that
...SET col1='value1' AND col2='value2'...
instead of
...SET COL1='value1', col2='value2'...
Could you post your stored procedure?