现在有一个表a是这样的:表序号 单序号 货品编号
1234 1 ******
1235 2 ******
1236 3 ******
我现在需要从表b Select出货品编号,然后插入到表a,表a的表序号和单序号没有自动+1
表b有几条数据,插入之后的效果是这样的:表序号 单序号 货品编号
1234 1 ******
1235 2 ******
1236 3 ******
1237 4 ******
1238 5 ******
主要就是控制表序号和单序号怎么在表a的基础上自动加1.
1234 1 ******
1235 2 ******
1236 3 ******
我现在需要从表b Select出货品编号,然后插入到表a,表a的表序号和单序号没有自动+1
表b有几条数据,插入之后的效果是这样的:表序号 单序号 货品编号
1234 1 ******
1235 2 ******
1236 3 ******
1237 4 ******
1238 5 ******
主要就是控制表序号和单序号怎么在表a的基础上自动加1.
select @i = max(表序号) from taupdate ta
set 表序号 = @i ,@I = @I +1
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([a] int,[b] varchar(2))
insert [tb]
select 1100,'aa' union all
select 1100,'bb' union all
select 1101,'cc' union all
select 1101,'dd' union all
select 1102,'ee' union all
select 1103,'ff'
---查询---
select
ltrim(a)+right('00'+ltrim((select count(1)+1 from tb where a=t.a and b<t.b)),2) as a,
b
from
tb t---结果---
a b
---------------- ----
110001 aa
110002 bb
110101 cc
110102 dd
110201 ee
110301 ff(所影响的行数为 6 行)
ltrim这个函数没用过啊,看不懂。