0

I am using MySql and writing the code below as part of my stored procedure:

  SET @label_id_configuration = 0;
  IF NOT (SELECT count(1) FROM `system_labels` WHERE  `name` = 'configuration') THEN
    SELECT @label_id_configuration := Max(`label_id`) + 1 FROM   `system_labels`;
    INSERT INTO `system_labels` (`label_id`, `name`) VALUES (@label_id_configuration, 'Configuration');
  END IF;

It runs fine on my local system. But on the build server, it fails. The rest of the stored procedure runs fine, which means we don't have permission, database, table existence problems.

Could anyone give me a hint to troubleshoot the error message received on the server?

Error on or near line 151: error in SQL script data_update.sql: @label_id_configuration:=`label_id` 6   
2
  • why don't you try INSERT INTO system_labels (label_id, name) SELECT Max(label_id) + 1, 'Configuration' FROM system_labels. Commented Dec 19, 2014 at 14:35
  • also what does your if condition supposed to check? Commented Dec 19, 2014 at 14:38

2 Answers 2

1

I don't know why it's erroring, but I can see how you can eradicate the use of that variable altogether (given the code shown):

IF NOT EXISTS(SELECT * FROM `system_labels` WHERE `name` = 'configuration') THEN
    INSERT INTO `system_labels` (`label_id`, `name`)
    SELECT Max(`label_id`) + 1, 'Configuration' FROM `system_labels`
END IF;
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks all for your help.

After lots of investigation with no luck. I decided to use Insert Ignore and Replace instead of the script above.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.