自动递增字符串,如Cus01,Cus02,Cus03 ... [英] Auto increment with string like Cus01,Cus02,Cus03......

查看:64
本文介绍了自动递增字符串,如Cus01,Cus02,Cus03 ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,
我想在像CUS01,cus02这样的数据库中增加字段值.

Sir,
I want to increment my Field Value in databaselike CUS01,cus02..

推荐答案

好,您尝试了什么?您的值是字符串吗?它们无法自动递增,您必须编写代码才能做到这一点,例如触发器?您没有告诉我们任何事情,因此很难知道您想要什么.
OK, well, what have you tried ? Your values are strings ? They can''t auto increment, you''d have to write code to do it, like perhaps a trigger ? You''ve failed to tell us much of anything, so it''s hard to know what you want.


通常,您不应该考虑以这种方式进行操作.或者,您可能要考虑的是仅具有一个自动递增的值,然后可以在检索到它之后将其添加到前缀字符串中.
Typically you shouldn''t think of doing this in this way. What you might want to consider, as an alternative, is to just have an auto incrementing value, and then you can just add this to the prefix string after you''ve retrieved it.


并不是那么简单:字符串不能仅由整数自动递增.另外还有其他问题.

我假设您要创建客户代码,例如CUS01,CUS02,ANC01,ANC02,因此您将必须弄清楚当您已经拥有CUS99并想要下一个代码时会发生什么.问题.

有两种安全的方法:将INSERT移动到存储过程中,并在那里计算新值,或者创建一个存储过程,该存储过程包含"CUS"部分并返回下一个可用名称.为了支持这一点,我将有一个单独的表,其中包含CUS部分的字符串,以及该字符串使用的最后一个值.存储过程读取该行,更新该值,然后在返回新字符串完成之前将其写回.

我?我会走第二条路线.
It''s not that simple: strings cannot be autoincremented by the database only integers. Plus there are other problems.

I assume that you want to create customer codes such as CUS01, CUS02, ANC01, ANC02, so you will have to work out what happens when you already have CUS99 and want the next one... You also have to be very careful about multiuser problems.

There are two safe ways to do it: moving your INSERTs to a stored procedure, and working out the new value there, or create a stored procedure which takes the "CUS" part and returns the next available name. To support this, I would have a separate table containing a string for the CUS part, and the last value used for that string. The stored procedure reads the row, updates the value, and writes it back befoire returnign teh new string complete.

Me? I would go the second route.


这篇关于自动递增字符串,如Cus01,Cus02,Cus03 ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆