Fixing "No data - zero rows fetched, selected, or processed" warning - mysql

The below function works fine except it throws a warning 'No data - zero rows fetched, selected, or processed (errno. 1329)'. and since i start using this function with django there cant be any warning or error because it stop the whole process
any idea how to fix this?
DELIMITER $$
DROP FUNCTION IF EXISTS objtree_node_add $$
CREATE FUNCTION objtree_node_add(i_name VARCHAR(255), i_parent_id BIGINT, i_type_id BIGINT) RETURNS bigint(20)
BEGIN
DECLARE a_name VARCHAR(255);
IF NOT i_name RLIKE '^[a-zA-Z0-9_-]+$' THEN
RETURN -1;
END IF;
SELECT name INTO a_name FROM objtree_nodes WHERE parent_id = i_parent_id AND name = i_name;
IF NOT a_name IS NULL THEN
RETURN -5;
END IF;
...

I don't know where you read that there is no warnings filtering in Django. Django is just Python, so you can use the Python warnings module.
import warnings
warnings.filterwarnings("ignore", "No data .*")

import warnings
warnings.filterwarnings("ignore", "No data .*")

Related

MySQL database (8.0.30) - Error Code: 1241. Operand should contain 1 column(s) - Error on stored procedure and cursor

I'm struggling with a MySQL 1241 "Operand should contain 1 column(s)" error.
This is a test stored procedure. I cannot figure out where's the problem. I'm learning MySQL, thanks!
DECLARE c_Property_URL varchar(250) DEFAULT "";
DECLARE finished int DEFAULT 0;
-- declare cursor for property URL
DECLARE curPropertyURL CURSOR FOR SELECT URL FROM `dbmaster`.`KIC_TB_Property`;
-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = 1;
OPEN curPropertyURL;
getPropertyURL: LOOP
FETCH curPropertyURL INTO c_Property_URL;
SELECT ('property URL is: ',c_Property_URL);
IF finished = 1 THEN
LEAVE getPropertyURL;
END IF;
END LOOP getPropertyURL;
CLOSE curPropertyURL;
You seem to be using tuple syntax in your select-list:
SELECT ('property URL is: ',c_Property_URL);
I assume you intended this to be string concatenation, but this is not MySQL's syntax for string concatenation. If you want that, use:
SELECT CONCAT('property URL is: ',c_Property_URL);
Tuple syntax is allowed in some SQL expressions, but not for scalar expressions in the select-list.

Want to generate unique Id's using a function in mysql

I wrote a function to generate unique id's,its working but sometimes two people are getting same id,I mean duplicates are formed. My unique id looks like
2016-17NLR250001, I deal with only last four digits 0001. I am posting my function please correct it and please help me in avoiding duplicates even though users login into same account or if they do it on same time.
MY FUNCTION:
DELIMITER $$
USE `olmsap`$$
DROP FUNCTION IF EXISTS `fun_generate_uniqueid`$$
CREATE DEFINER=`root`#`%` FUNCTION `fun_generate_uniqueid`( V_DATE DATE,V_MANDALID INT ) RETURNS VARCHAR(30) CHARSET latin1
DETERMINISTIC
BEGIN
DECLARE MDLCODE VARCHAR(5);
SET MDLCODE = ' ';
SELECT COUNT(*) INTO #CNT FROM `st_com_mandal` WHERE MANDAL_VS_MC=V_MANDALID;
SELECT dist_mandal_code INTO MDLCODE FROM `st_com_mandal` WHERE MANDAL_VS_MC=V_MANDALID;
IF #CNT>0 THEN
SET #YR=`FUN_FISCAL_YR`(V_DATE);
SELECT CONCAT(IF(DIST_SAN_CODE='GUN','GNT',DIST_SAN_CODE),IFNULL(`dist_mandal_code`,'NULL'))INTO #MANDAL
FROM `st_com_dist` SCD INNER JOIN `st_com_mandal` STM ON STM.`mandal_dist_id`= SCD.`DIST_VC_DC` WHERE MANDAL_VS_MC=V_MANDALID;
IF MDLCODE >0 THEN
SELECT COUNT(Soil_Sample_ID)+1 INTO #ID FROM `tt_mao_soil_sample_dtls` WHERE MANDAL_ID=V_MANDALID AND SUBSTR(UNIQUE_ID,1,7)=#YR ;
ELSE
SELECT COUNT(Soil_Sample_ID)+1 INTO #ID FROM `tt_mao_soil_sample_dtls` WHERE SUBSTR(UNIQUE_ID,1,14)=CONCAT(#YR,#MANDAL) ;
END IF ;
IF LENGTH(#ID)=1 THEN
SET #ID=CONCAT('000',#ID);
ELSEIF LENGTH(#ID)=2 THEN
SET #ID=CONCAT('00',#ID);
ELSEIF LENGTH(#ID)=3 THEN
SET #ID=CONCAT('0',#ID);
ELSE
SET #ID=#ID;
END IF ;
RETURN CONCAT(#YR,#MANDAL,#ID);
ELSE
RETURN 'Mandal Doesnt Exists';
END IF;
END$$
DELIMITER ;
I do not think community will be able to help you with this question. This is a complex function that requires very careful analysis of table / index access and locking.
The only thing I can recommend is to not use existing table data to calculate next sequence as this is a bad practice.
Besides Race conditions that you are experiencing you will also get problems if the record with the last sequence is deleted.
I suggest you read this to get an idea on how to write a custom sequence generator:
http://en.latindevelopers.com/ivancp/2012/custom-auto-increment-values/

Postgresql - "Returning" the "inserted" row as json after an Insert

I'm trying to use the "RETURNING" keyword after an INSERT statement to allow to return the inserted row data as json - I want all the fields. Here is what I tried with no success:
CREATE OR REPLACE FUNCTION createUser(
email_f character varying(50),
nickname_f character varying(16)
) RETURNS json AS $$
BEGIN
INSERT INTO users (email,nickname)
SELECT email_f,nickname_f
RETURNING row_to_json(row(*));
END;
$$ LANGUAGE 'plpgsql';
This gives me an error: ERROR: syntax error at or near "*"
What could I be doing wrong here? I would like to continue using the "Returning" keyword as it seems to be the best way to go about it - provided I can get it to work...
You may have more success with:
row_to_json(users.*)
You can do it properly using a middle variable:
CREATE OR REPLACE FUNCTION createUser(
email_f character varying(50),
nickname_f character varying(16)
) RETURNS json AS $$
declare temp_variable users%rowtype;
BEGIN
INSERT INTO users (email,nickname)
SELECT email_f,nickname_f
RETURNING *
into temp_variable;
return row_to_json(temp_variable);
END;
$$ LANGUAGE 'plpgsql';
RETURNING *

Log exception info in MySQL stored procedure

As I know, I can define exception handler in MySQL stored procedure, but seems I can't catch the exception message in the handler and write a log in a table for debugging purpose. I just want to know is there method to log exception code and message in MySQL store procedure?
You can catch the message, the error code, the sql state, ..., with the GET DIAGNOSTICS statement, in 5.6.4
See
http://dev.mysql.com/doc/refman/5.6/en/get-diagnostics.html
I don't remember what tutorial I copied this from. However, it has helped immensely in the versions of MySQL prior to 5.6. Thanks to whomever I learned this from!
Step 1 : Create a debug_log table. This will hold everything your write to it.
CREATE TABLE `debug_log` (
`debug_log_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`msg` varchar(512) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`debug_log_id`)
) ENGINE=MyISAM
Step 2 : Create a stored procedure for adding info to the debug_log table.
DELIMITER $$
USE `your_db_name_here`$$
DROP PROCEDURE IF EXISTS `log_debug`$$
CREATE DEFINER=`ss7admin`#`%` PROCEDURE `log_debug`(IN lastMsg VARCHAR(512))
BEGIN
INSERT INTO debug_log (msg) VALUES (lastMsg);
END$$
DELIMITER ;
Step 3 : Add a handler in your real stored procedure.
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
CALL log_debug(
CONCAT
(
now(),
' : Something went horribly wrong!',
'.'
)
);
CALL log_debug('Exiting now');
SET outReturnCode = 1;
END;
You cannot catch the message, but you can catch the error code.
Here is an example of how to deal with "Duplicate entry" (PK, UK constraint):
CREATE PROCEDURE sp_do_insert(
IN in_param1 int,
IN in_param2 int,
OUT out_status tinyint
)
BEGIN
DECLARE CONTINUE HANDLER FOR 1062 SET out_status = 1;
SET out_status = 0;
INSERT INTO tbl(field1, fiel2)
VALUES (in_param1, in_param2);
END;
If tbl has a UK constraint on field1 and you try to insert an existing value once again you will not get an error. Nothing will be inserted and status will be equal to 1.
You can also add other handlers for other error codes. And you will always know what is the error from out_status value and you will know "error message" from error_code (in handler).
You can try to play with show warnings (it shows errors/warnings for the last query) in case if out_status <> 0.
Hope it helps.

Problem with my first MySQL Function. Getting one error after the other

So, I'm making my first MySQL function.
BEGIN
DECLARE ID INT;
SELECT
LandID INTO ID
FROM
Landen
WHERE
Landnaam = landnaam;
RETURN ID;
END
The parameter is a varchar.
So, let's say I got a record in Landen with the Landnaam 'Nederland'.
I run my function, give 'Nederland' as parameter.
But then it gives me to following error:
1172 - result consisted of more than one row
Which makes no sense at all. Cause when I give something as a parameter that is not in my database, like 'ASDASD', I get the same error.
And when limmiting the results with
LIMIT 1
It just always returns 1.
What am I doing wrong?
drop function if exists myfunc;
delimiter //
create function myfunc(str varchar(50))
returns int
reads sql data
begin
declare id int;
select landid into id from Landen where landnaam = str limit 1;
return id;
end//
delimiter ;
select myfunc('nederland');