Jump to content
New Reality: Ads For Members ×

how to use enum parameters in mysql stored procedure


Go to solution Solved by shadd,

Recommended Posts

i have this procedure:

DELIMITER $$
CREATE PROCEDURE sp_add_user(IN inname text,IN insex tinytext)
	MODIFIES SQL DATA
BEGIN
set @newEanswer_sql=CONCAT('INSERT INTO user(fname,sex)values("',inname,'","',insex,'")'  );
			PREPARE snewEans_insert FROM @newEanswer_sql;
			EXECUTE snewEans_insert;
			DEALLOCATE PREPARE snewEans_insert;
	END$$
DELIMITER ;

when i run it like so:

call sp_add_user('pierre','');

it gives me the following error:

RETURNED_SQLSTATE MESSAGE_TEXT
01000 Data truncated for column 'sex' at row 1

here is table:

create table user
(
	userid	int unsigned not null auto_increment,
	fname  	text  ,
	
	sex  enum('male','female'),

	Primary key(userd)
);

 

how can i solve this??

That procedure is wacky. Why is it trying to make a simple INSERT be so complicated? And so prone to not working?

The error is telling you, in a weird way that doesn't seem very obvious, that the value you used is not valid. Which it clearly isn't.

  • Haha 1
8 minutes ago, requinix said:

That procedure is wacky. Why is it trying to make a simple INSERT be so complicated? And so prone to not working?

The error is telling you, in a weird way that doesn't seem very obvious, that the value you used is not valid. Which it clearly isn't.

what if i need to account for null entries to this field

 

  • Solution
13 minutes ago, requinix said:

Don't overthink the procedure so much and just have it do the absolute most obvious thing: an INSERT statement.

SOLVED IT BY ADDING IF INSEX<>''THEN

DO INSERT WITH THE ENUM FIELD

ELSE

DO INSERT WITHOUT THE ENUM FIELD IN QUERY

END IF;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.