update 某一字段自动累加如何

ww111222 2009-04-16 12:27:32
我有一个表,

字段 TNum 是一个 数值字段,


我想让它从 650001开始自动增加,共蒸发30000条记录,


应该如何写这个UPDATE?


您指点一下?
...全文
1450 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
百年树人 2009-04-16
  • 打赏
  • 举报
回复
try--
declare @i bigint 
set @i=650000120001
update driver_repair set nationalnum=ltrim(@i),@i=ltrim(@i+1)

select * from driver_repair
ww111222 2009-04-16
  • 打赏
  • 举报
回复
ww111222 2009-04-16
  • 打赏
  • 举报
回复
declare @i int
set @i=650000120001
update driver_repair set nationalnum=ltrim(@i),@i=ltrim(@i+1)

select * from driver_repair


服务器: 消息 8115,级别 16,状态 2,行 2
将 expression 转换为数据类型 int 时发生算术溢出错误。

(所影响的行数为 5111 行)


(所影响的行数为 5111 行)


这是为什么呀?
htl258_Tony 2009-04-16
  • 打赏
  • 举报
回复
nvarchar也没问题,用ltrim将其隐式转为字符型就行了。
htl258_Tony 2009-04-16
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 ww111222 的回复:]
引用 9 楼 ws_hgo 的回复:
SQL codedeclare @i int
set @i=1
--650001开始自动增加,共蒸发30000条记录,
while (@i <=30000)
begin
insert into tb (ID) values (650001+@i)
set @i=@i+1
end



谢谢这位大哥了, 我是 update,而且这个字段是字符型的
[/Quote]

if object_id('tb') is not null
drop table tb
go
create table tb([商品id] varchar(10),[进库日期] datetime,TNum varchar(20))
insert tb select '1','2009-1-20',200
insert tb select '1','2009-1-1',100
insert tb select '2','2009-2-1',50
insert tb select '2','2009-3-2',200
insert tb select '2','2009-3-3',10
insert tb select '2','2009-3-1',160
go
declare @i int
set @i=650000
update tb set Tnum=ltrim(@i),@i=ltrim(@i+1)

select * from tb
/*
商品id 进库日期 TNum
---------- ----------------------- -----------
1 2009-01-20 00:00:00.000 650001
1 2009-01-01 00:00:00.000 650002
2 2009-02-01 00:00:00.000 650003
2 2009-03-02 00:00:00.000 650004
2 2009-03-03 00:00:00.000 650005
2 2009-03-01 00:00:00.000 650006

(6 行受影响)
*/
ww111222 2009-04-16
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ws_hgo 的回复:]
SQL codedeclare @i int
set @i=1
--650001开始自动增加,共蒸发30000条记录,
while (@i<=30000)
begin
insert into tb (ID) values (650001+@i)
set @i=@i+1
end
[/Quote]



谢谢这位大哥了, 我是 update,而且这个字段是字符型的
ws_hgo 2009-04-16
  • 打赏
  • 举报
回复
declare @i int
set @i=1
--650001开始自动增加,共蒸发30000条记录,
while (@i<=30000)
begin
insert into tb (ID) values (650001+@i)
set @i=@i+1
end
ww111222 2009-04-16
  • 打赏
  • 举报
回复
这个字段是 nvarchar 的字段属性
claro 2009-04-16
  • 打赏
  • 举报
回复
帮顶。
ww111222 2009-04-16
  • 打赏
  • 举报
回复



不好意思, 五笔打字错误,


是这样的,


我的表在设计的时候有问题,后面想增加一个字段,把 650001写进去,

现在已经有 3000行数据了,

所以,想手工增加
htl258_Tony 2009-04-16
  • 打赏
  • 举报
回复
if object_id('tb') is not null
drop table tb
go
create table tb([商品id] varchar(10),[进库日期] datetime,TNum int)
insert tb select '1','2009-1-20',200
insert tb select '1','2009-1-1',100
insert tb select '2','2009-2-1',50
insert tb select '2','2009-3-2',200
insert tb select '2','2009-3-3',10
insert tb select '2','2009-3-1',160
go
declare @i int
set @i=650000
update tb set Tnum=@i,@i=@i+1

select * from tb
/*
商品id 进库日期 TNum
---------- ----------------------- -----------
1 2009-01-20 00:00:00.000 650001
1 2009-01-01 00:00:00.000 650002
2 2009-02-01 00:00:00.000 650003
2 2009-03-02 00:00:00.000 650004
2 2009-03-03 00:00:00.000 650005
2 2009-03-01 00:00:00.000 650006

(6 行受影响)
*/
ww111222 2009-04-16
  • 打赏
  • 举报
回复

对了, 这个表有一个ID 字段是自动序列号累加的
htl258_Tony 2009-04-16
  • 打赏
  • 举报
回复
declare @i int
set @i=650001
update tb set Tnum=@i,@i=@i+1



共蒸发30000条记录,这句话不明白。
sdhdy 2009-04-16
  • 打赏
  • 举报
回复
--既然是从650001开始自动增加,为何还要写UPDATE?
--可以将该字段设为标识字段,起始值为650000,步长、增长为1,以后向表里插数据的时候,不插这列,数据会自动生成的。
百年树人 2009-04-16
  • 打赏
  • 举报
回复
是insert还是update?
htl258_Tony 2009-04-16
  • 打赏
  • 举报
回复
declare @i bigint 
set @i=650000120000 --应该从这个数开始
update driver_repair set nationalnum=ltrim(@i),@i=ltrim(@i+1)

select * from driver_repair
ww111222 2009-04-16
  • 打赏
  • 举报
回复
谢谢,成功了
htl258_Tony 2009-04-16
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 ww111222 的回复:]

[/Quote]

declare @i bigint 
set @i=650000120001
update driver_repair set nationalnum=ltrim(@i),@i=ltrim(@i+1)

select * from driver_repair

34,591

社区成员

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

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