I’m trying to create stored procedure in MySQL Community Server 5.5.16:
CREATE UNIQUE SEQUENCE seq_num_accountnumber;
Create Procedure usp_createCustomerAccount(IN pCustomer INTEGER,pCredit INTEGER)
NOT DETERMINISTIC
MODIFIES SQL DATA
BEGIN
DECLARE Z INTEGER;
set Z = NEXT_VALUE OF seq_num_accountnumber;
if pCredit is null then
Insert into Account(Number,Customer,Credit) Values(Z,pCustomer,0);
else
Insert into Account(Number,Customer,Credit) Values(Z,pCustomer,pCredit);
End if;
End
But I do not know how to use UNIQUE SEQUENCE in MySQL Server. I tried like this but I think it is incorrect because the value is not unique:
Create Procedure my_createCustomerAccount(IN pCustomer INTEGER,pCredit INTEGER)
NOT DETERMINISTIC
MODIFIES SQL DATA
BEGIN
DECLARE Z INTEGER;
set Z = 1;
if pCredit is null then
Insert into Account(Number,Customer,Credit) Values(Z,pCustomer,0);
else
Insert into Account(Number,Customer,Credit) Values(Z,pCustomer,pCredit);
set Z = Z+1;
End if;
End
I have just set the field
Number as NOT NULL AUTO_INCREMENTand in the insert sqlINSERT INTO Account(Customer, Credit) VALUES(pCustomer,0);