0

I have some trouble with MySql Procedure. I have:

DROP TABLE IF EXISTS `employees2`;
CREATE TABLE `employees2` (
  `LastName` varchar(20) character set utf8 collate utf8_unicode_ci NOT NULL default '',
  `FirstName` varchar(10) character set utf8 collate utf8_unicode_ci NOT NULL default ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

It work's and is OK.

And MySQL Procedure:

DROP PROCEDURE IF EXISTS gen;

DELIMITER $$
CREATE PROCEDURE gen()
    BEGIN
        DECLARE e1 TEXT;
        DECLARE e2 TEXT;
        DECLARE e3 TEXT;
        SET e1 = "Davolio";
        SET e2 = "Nancy";
        SET e3 = "Ron , Deplin";
        insert  into `employees2`(`LastName`,`FirstName`) values ('Nonew','adams');
        insert  into `employees2`(`LastName`,`FirstName`) values (e1,e2);
        insert  into `employees2`(`LastName`,`FirstName`) values (e3);

    END $$
DELIMITER ;

call gen();

I would like to instert into table values from variable e3. "Ron" is for column LastName and "Deplin" is for column FirstName. But i got error: "Error Code: 1136. Column count doesn't match value count at row 1" First and second inserts works fine. How to force the third insert to work ?

1 Answer 1

0

You're only specifying one value with two columns named in your last insert, hence the unequal row count.

See this post on how to use MySQL to split a string like that got two value entries: https://stackoverflow.com/a/9953163/2812842

4
  • Yes. But i have: SET e3 = "Ron , Deplin"; And Ron is for LastName column and Deplin is for FirstNamecolumn. I just would like paste this e3 between () in insert query.
    – marooned
    Nov 15, 2013 at 20:31
  • Ok. insert into employees2(LastName,FirstName) values (SUBSTRING_INDEX(e3, ',',1),SUBSTRING_INDEX(e3, ',',-1)); It's works but it's not what i want. When i will have longer list of values this method will be useless.
    – marooned
    Nov 15, 2013 at 20:37
  • Do you need to split a string like that? The more data you have, the more you should be using separate fields or structured data...
    – scrowler
    Nov 15, 2013 at 20:39
  • OK. But what if: insert into employees2(LastName,FirstName,City,Country) values (e1,e2); where my e1 = "Ron , Deplin" and e2 = "NY, USA" I tried change e3 on many ways and it did not pass. For example: e3 = "'Ron' , 'Deplin'" etc @scrowler - maybe i'am not clear. Do you understood what im want to do ?
    – marooned
    Nov 15, 2013 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.