Jump to content

[SOLVED] [Mysql 5.1] Stored Procedures (variables)


Hooker

Recommended Posts

I'm trying to check whether or not phone numbers in a particular column have "0" at the start of them, within a stored procedure so to check i've written:

 

  DECLARE jobid_p INT;

      SET @Checknumber = CONCAT("SELECT LEFT(",jobname_p,".",jobphoneno_p,", 1) INTO ",checknum," FROM ",jobname_p," LIMIT 10, 1;");
      PREPARE Checknumber FROM @Checknumber;
      EXECUTE Checknumber;

      IF checknum= '0' THEN
          -- DO SOMETHING
      ELSE
          -- DO SOMETHING ELSE
       END IF;

 

Which i hoped would retrieve the 10th record (far enough into the file not be a header, not far enough to be the end) from a dynamicaly named column (hence the messey setup) and return the first character from that record so i could then do a simple IF statement on the variable "checknum" to see if its 0 or not. Unfortunately it keeps returning NULL.

 

I've taken the query out of the CONCAT and ran it as a normal query (minus variables) and it returns fine, i know there isn't an issue with the variables because they're used in other areas of the procedure, i just can't seem to figure out what's causing the problem.

Link to comment
Share on other sites

first off i popped the wrong variable declaration in my example of what i was trying to do but beside that, i found this worked perfectly:

 

      SET @checknum NULL;
      SET @Checknumber = CONCAT("SELECT LEFT(",jobname_p,".",jobphoneno_p,", 1) INTO @checknum FROM ",jobname_p," LIMIT 10, 1;");
      PREPARE Checknumber FROM @Checknumber;
      EXECUTE Checknumber;

      IF @checknum= '0' THEN
          -- DO SOMETHING
      ELSE
          -- DO SOMETHING ELSE
       END IF;

Link to comment
Share on other sites

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.