一个关于自动增长的问题,进者有分...

白云任去留 2008-08-29 07:09:33
sql server 2000数据库,某表中有个字段(编号)ordernum ,字段类型为nvarchar,格式为GH10000,现在每增加一条记录,就在以上基础上加1,即GH10001 , GH10002 ,GH10003...不知有什么好的方法可以插入一条数据后,实现上述自动增长。望各位帮帮忙,先谢谢了...
...全文
320 40 打赏 收藏 转发到动态 举报
写回复
用AI写文章
40 条回复
切换为时间正序
请发表友善的回复…
发表回复
stepman 2008-08-31
  • 打赏
  • 举报
回复
可以在库表中先去掉GH, 使用的时候再加上,这样不就即不使用触发器,也不用犯难了。土办法,哈哈
pdsnet 2008-08-31
  • 打赏
  • 举报
回复
顶下..原来我弄的那么复杂
xie_yanke 2008-08-30
  • 打赏
  • 举报
回复
declare @t table(ordernumber varchar(7))
insert @t select (case when exists(select * from @t) then (select 'GH' + convert(varchar, (convert(int, isnull(substring(isnull(ordernumber, 'GH10000') , 3, len(isnull(ordernumber, 'GH10000')) - 2), 10000)) + 1)) from @t) else 'GH10000' end)
select * from @t
白云任去留 2008-08-30
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 winner2050 的回复:]
引用 13 楼 suyiming 的回复:
我倒 这要用触发器?
=============

插入的时候
在存储过程见id加1就行了


SQL codedeclare @id int
select @id=cast((select max(Id) from table) as int)

insert into table(id) value(@id+1)


declare @id int
select @id=max(Id)+1 from table

insert into table(id) value(@id)

这样更加简洁
[/Quote]

那是nvarchar类型的,你这样怎么能行呢
tkscascor 2008-08-30
  • 打赏
  • 举报
回复
10楼 正解
LQknife 2008-08-30
  • 打赏
  • 举报
回复
接分 晓习
hhaiok 2008-08-30
  • 打赏
  • 举报
回复
不要把一个东西弄的太复杂,要考虑维护成本
winner2050 2008-08-30
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 suyiming 的回复:]
我倒 这要用触发器?
=============

插入的时候
在存储过程见id加1就行了


SQL codedeclare @id int
select @id=cast((select max(Id) from table) as int)

insert into table(id) value(@id+1)
[/Quote]

declare @id int
select @id=max(Id)+1 from table

insert into table(id) value(@id)

这样更加简洁
shadowjl 2008-08-30
  • 打赏
  • 举报
回复
mark
konglingkang 2008-08-30
  • 打赏
  • 举报
回复
可以写个方法让前面的字母固定然后使用“+”串联上后面的数字【递增】即可!
jiangpingchang 2008-08-30
  • 打赏
  • 举报
回复
10楼正解
accomp 2008-08-30
  • 打赏
  • 举报
回复
学习了
lin5619sunn 2008-08-30
  • 打赏
  • 举报
回复
看不懂 帮顶
niuniuhuang 2008-08-30
  • 打赏
  • 举报
回复
个人不喜欢用触发器
在代码里面写
xl_nj 2008-08-30
  • 打赏
  • 举报
回复
触发器
suyiming 2008-08-30
  • 打赏
  • 举报
回复
我倒 这要用触发器?
=============

插入的时候
在存储过程见id加1就行了

declare @id int
select @id=cast((select max(Id) from table) as int)

insert into table(id) value(@id+1)
locoasta 2008-08-30
  • 打赏
  • 举报
回复
10楼的老大帮你写好了。。。嘿嘿
我以前遇到这样的都是专门写了段代码处理流水号的
MadWork 2008-08-30
  • 打赏
  • 举报
回复
GH+int型增长,写个函数,具体的我也没写过,只直到这个方法
xiaoxiong5227 2008-08-30
  • 打赏
  • 举报
回复
学习
toxxj 2008-08-30
  • 打赏
  • 举报
回复
insert table(ordernum) VALUES @ordernum
加载更多回复(20)

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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