I am using below code to find string. If entered string not matched then cursor is pointing at last record. But i have to display message "No data found".
BEGIN
IF :BLOCK.DESC IS NULL THEN
ERR_MESSAGE('Please enter name OR Description...');
SET_ITEM_PROPERTY('BLOCK.DESC',ENABLED,PROPERTY_TRUE);
GO_ITEM('BLOCK.DESC');
ELSE
BEGIN
GO_BLOCK('CCC_MST');
FIRST_RECORD;
LOOP
IF :CCC_MST.DESC like '%'||:BLOCK.DESC||'%'
OR :CCC_MST.CC LIKE '%'||:BLOCK.DESC||'%' THEN
EXIT;
END IF;
NEXT_RECORD;
EXIT WHEN :SYSTEM.LAST_RECORD='TRUE';
END LOOP;
END;
END IF;
END;
Can you please help me?
Thanks
I got the answer..
if ...then
v_counter:=count+1;
end if;
end loop;
if v_counter<1 then
message('no data found');
end if;
Related
In a stored procedure, I can have a continue handler that handles errors. Can I somehow access the details of the respective error inside the handler?
Small example:
DROP PROCEDURE IF EXISTS `DO_SOMETHING_WRONG`;
DELIMITER $$
CREATE PROCEDURE `DO_SOMETHING_WRONG`()
BEGIN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Hey, this is an error!';
END $$
DELIMITER ;
(That's obviously the failing part of the example)
DROP PROCEDURE IF EXISTS `CALL_SOMETHING_WRONG`;
DELIMITER $$
CREATE PROCEDURE `CALL_SOMETHING_WRONG`()
BEGIN
DECLARE ERROR_OCCURRED BOOLEAN DEFAULT FALSE;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET ERROR_OCCURRED := TRUE;
CALL DO_SOMETHING_WRONG();
IF ERROR_OCCURRED IS TRUE THEN
SELECT 'An error occurred!' AS Output;
ELSE
SELECT 'No error occurred!' AS Output;
END IF;
END $$
DELIMITER ;
When I call CALL CALL_SOMETHING_WRONG(); it gives me An error occurred! as it should, but can I let it tell me, what error? So, get hold of the 45000 and the Hey, this is an error! in this example?
CREATE OR REPLACE PROCEDURE get_productdetails(
p_reqid OUT requirement.req_id%type,
p_pid OUT requirement.p_id%type,
p_rstaffid OUT requirement.r_staff_id%type,
p_demand requirement.demand%type)
IS
CURSOR c_demand IS
SELECT req_id,p_id,r_staff_id from requirement where demand = upper(p_demand);
BEGIN
FOR i in c_demand Loop
DBMS_OUTPUT.PUT_LINE('Requirement ID :'||i.req_id);
DBMS_OUTPUT.PUT_LINE('Product ID'||i.p_id);
DBMS_OUTPUT.PUT_LINE('Staff ID :'||i.r_staff_id);
END LOOP;
END get_productdetails;
User must enter demand in 'HIGH/LOW/AVG only otherwise it should throw exception asking to enter data in that format
Can you please help me to write an exception accordingly
As I don't have access to your table structure, the code below is untested. A good place to start is the official oracle documentation. In your case, a user-defined exception is needed, which you need to declare in the declaration section, then RAISE and finally catch in the EXCEPTION block.
CREATE OR REPLACE PROCEDURE get_productdetails(
p_reqid OUT requirement.req_id%type,
p_pid OUT requirement.p_id%type,
p_rstaffid OUT requirement.r_staff_id%type,
p_demand requirement.demand%type)
IS
-- declare the user-defined exception
invalid_input EXCEPTION;
CURSOR c_demand IS
SELECT req_id,p_id,r_staff_id from requirement where demand = upper(p_demand);
BEGIN
IF p_demand NOT IN ('HIGH','LOW','AVG') THEN
-- raise the exception if p_demand not one of the 3 accepted values
RAISE invalid_input;
END IF;
FOR i in c_demand Loop
DBMS_OUTPUT.PUT_LINE('Requirement ID :'||i.req_id);
DBMS_OUTPUT.PUT_LINE('Product ID'||i.p_id);
DBMS_OUTPUT.PUT_LINE('Staff ID :'||i.r_staff_id);
END LOOP;
EXCEPTION WHEN invalid_input THEN
dbms_output.put_line('Only HIGH/LOW/AVG are valid input');
END get_productdetails;
I'm new at this so please bear with me. I've created the following stored procedure.
DELIMITER //
CREATE PROCEDURE REMARK()
BEGIN
#declare variable
DECLARE v_newid, v_oldid VARCHAR(255);
DECLARE done INT DEFAULT FALSE;
#declare cursor
DECLARE cur1 CURSOR FOR
SELECT new, old
FROM mydb.`tbl_id`;
#declare handle
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
#open cursor
OPEN cur1;
#starts the loop
the_loop: LOOP
#get the values of each column into our variables
FETCH cur1 INTO v_newid, v_oldid;
IF done THEN
LEAVE the_loop;
END IF;
#Insert it
INSERT INTO cherrycasino.`tbl_remarks` (player_id, user_tool_id, text)
VALUES (v_newid, '103', 'User Copied from ES id:v_oldid');
END LOOP the_loop;
CLOSE cur1;
END //
DELIMITER ;
It is working as it should except for one thing. I want to insert some text in tbl_remarks using a variable I got from the cursor.
VALUES (v_newid, '103', 'User Copied from ES id:v_oldid');
I cannot transform back the variable v_oldid back. Am I missing some escape sequence here?
Turns out the answer was a simple concat of the string I want to use plus the variable.
VALUES (v_newid, '103', CONCAT('User Copied from ES id:', v_oldid));
I have been receiving always an error while creating mysql function in cpanel with below code.
But the below code is running properly while creating mysql function in SQL Manger 2007.
Anyone tell me what is wrong with this code for cpanel.
ERROR :- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 12
(0.81 sec)
CREATE FUNCTION F_test (topsbwynum INTEGER)
RETURNS float(20,2)
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
BEGIN
DECLARE FinalPrice VARCHAR(20);
DECLARE err VARCHAR(20);
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET err = 1;
IF(sbwynum > 0)THEN
IF(FinalPrice > 0)THEN
RETURN FinalPrice;
ELSEIF(adjprice > 0)THEN
RETURN adjprice;
ELSEIF(price > 0)THEN
RETURN price;
ELSEIF(estprice > 0)THEN
RETURN estprice;
ELSE
RETURN 0;
END IF;
ELSE
RETURN 0;
END IF;
END;
Thanks a lot.
For multi-line statements, you can't use the same character (";", by default) to delimit the individual statements within the function definition and the entire CREATE statement. If you try, the creation statement ends at the first ";" (in the sample code, it's after the first DECLARE). Since the statement syntax error is at the end of the truncated statement, the error message gives '' as the part of the statement where the error occurs.
From the command line client, you'd use the delimiter command to change the delimiter used by the client to (e.g.) ";;", then end the creation statement with this delimiter:
delimiter ;;
CREATE FUNCTION F_test (topsbwynum INTEGER)
...
END;;
delimiter ;
In phpMyAdmin, there's a "delimiter" field that fulfills the same purpose.
Inside MySQL I have written a Procedure where I want to run a loop like this:
While (Cond) Do
...(Body1)
...
If (Condition2)
continue ;
...(Body2)
...
end while ;
Under the while loop I want the full body to run in case where Condition2 is not met (ie Body1 and Body2).
Currently, when Condition 2 is met, it just executes Body1 and then continues(Check Cond in While and Continue looping.)
Can someone help with the proper syntax to perform the above?
BEGIN
WHILE cond1 DO
CALL body1;
IF (NOT cond2) THEN
CALL body2;
END IF;
END WHILE;
END;