求一个分配方案

handsomerun 2006-05-18 07:59:58
有一个字段,char(8)的

然后呢,格式是这样的ca000001,ca000002,……………………,
前面两个是字母,后面是一窜数字,然后我要根据数据库中最大的那个窜,生成一个新的

什么意思呢

就是说,比如现在有10条记录,是ca000001,ca000002然后一直到ca000010

那么下一次生成的就是ca000011

这个该怎么做呢,呵呵
...全文
107 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
godfather_wang 2006-05-19
  • 打赏
  • 举报
回复
能用max函数吗,对于字符窜可以不??
---------------------
当然可以
smalltalks 2006-05-19
  • 打赏
  • 举报
回复
用max函数就可以了。
handsomerun 2006-05-19
  • 打赏
  • 举报
回复
顶起来
handsomerun 2006-05-18
  • 打赏
  • 举报
回复
谢谢楼上的两个大哥

可能我没讲清楚

我是要知道10条记录中的最大的那个,就是ca000010那个

然后获得那个ca000010以后,再生成ca000011

我要在数据库外面生成,不是数据库里面生成

能用max函数吗,对于字符窜可以不??
lxzm1001 2006-05-18
  • 打赏
  • 举报
回复

create table a(col varchar(10))
insert a select 'ca000001'
union all select 'ca000002'
union all select 'ca000003'
union all select 'ca000004'
union all select 'ca000005'
union all select 'ca000006'
union all select 'ca000007'
union all select 'ca000008'
union all select 'ca000009'
union all select 'ca000010'
select * from a
select 'ca'+right('000000'+cast(cast(right(max(col),6) as int)+1 as varchar(10)),6) from a
xeqtr1982 2006-05-18
  • 打赏
  • 举报
回复
declare @t table(id char(8))
insert into @t select 'ca'+right(1000001+isnull(right(max(id),6),0),6) from @t
insert into @t select 'ca'+right(1000001+isnull(right(max(id),6),0),6) from @t
insert into @t select 'ca'+right(1000001+isnull(right(max(id),6),0),6) from @t
insert into @t select 'ca'+right(1000001+isnull(right(max(id),6),0),6) from @t
insert into @t select 'ca'+right(1000001+isnull(right(max(id),6),0),6) from @t
insert into @t select 'ca'+right(1000001+isnull(right(max(id),6),0),6) from @t
insert into @t select 'ca'+right(1000001+isnull(right(max(id),6),0),6) from @t
insert into @t select 'ca'+right(1000001+isnull(right(max(id),6),0),6) from @t
insert into @t select 'ca'+right(1000001+isnull(right(max(id),6),0),6) from @t

select * from @t
--可以写成函数,作为字段的默认值.

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧