lundi 30 novembre 2015

A query that creates a unique 10 digit number the leading digit must not be zero and the 2ndand 3rd wil represesnt a state

I need to write a query that creates a unique 10 digit number for my table named tbl_user_vital. The leading digit must not be zero and the 2nd & 3rd digit will represent a state.

Here is what I have so far:

DELIMITER @@
CREATE FUNCTION generate_unique_pin() RETURNS int(10)
BEGIN

DECLARE newpin INT;
DECLARE myloop BOOL;
SET myloop = 1;

WHILE myloop = 1 DO
   SET newpin = FLOOR(RAND() * 9999999();
   IF (SELECT COUNT(*) AS used db_younitive.tbl_user_vital.user_pin WHERE user_pin = newpin) = 0 THEN
      SET myloop = 0;
   END IF;
END WHILE;

RETURN newpin;
END@@
DELIMITER ;

And here is the error message I'm getting:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '();
   IF (SELECT COUNT(*) AS used db_younitive.tbl_user_vital.user_pin WHERE u' at line 9

It seems the problem is it is the newpin variable, it is not receiving any value. I cant figure out why..

Number Generated: 2140-1234-45

This will be entered in the mysql database using jquery.




Aucun commentaire:

Enregistrer un commentaire