declear @bno='PC0001' update temp set Pno=(select max(Pno)+1 from products where type = (select type from temp where 订单编号=@bno)) where 订单编号=@bno
Create Table Products(Pno Varchar(6), Type Int) GO Select 'Pc'+Right(10000+IsNull(Right(Max(Pno),4),0)+1,4) From Products Where Type='1' Insert Products Select 'Pc0001',1 Select 'Pc'+Right(10000+IsNull(Right(Max(Pno),4),0)+1,4) From Products Where Type='1' GO Drop Table Products --Result Pc0001 Pc0002
update temp
set Pno=(select max(Pno)+1 from products
where type = (select type from temp where 订单编号=@bno))
where 订单编号=@bno
GO
Select 'Pc'+Right(10000+IsNull(Right(Max(Pno),4),0)+1,4) From Products Where Type='1'
Insert Products Select 'Pc0001',1
Select 'Pc'+Right(10000+IsNull(Right(Max(Pno),4),0)+1,4) From Products Where Type='1'
GO
Drop Table Products
--Result
Pc0001
Pc0002
這樣應該不行的,Pno不是數字。