2012-01-29 85 views
0

我想在MySQL社区服务器5.5.16创建存储过程创建唯一的序列:在MySQL社区服务器

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 

但我不知道如何在MySQL服务器使用唯一的序列。我想这样的,但我认为这是不正确,因为该值不是唯一的:

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 
+0

MySQL不支持序列 – 2012-01-29 15:24:41

+0

是否有任何理由不要使用'AUTO_INCREMENT列'? – danihp 2012-01-29 15:28:39

+0

我不知道如何在存储过程中使用AUTO_INCREMENT并使其唯一。 – user1047517 2012-01-29 16:14:39

回答

0

我刚才设置字段Number as NOT NULL AUTO_INCREMENT的并且在插入SQL INSERT INTO Account(Customer, Credit) VALUES(pCustomer,0);

相关问题