新手求助!!!!!

kevinhu520 2008-06-10 07:23:46
求一条sql
比如:当type=1时。ID=50001 每次递增1
当type=2时。ID=60001 每次递增1
当type=3时。 ID=70001 每次递增1

谢谢
...全文
53 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
-狙击手- 2008-06-11
  • 打赏
  • 举报
回复
看楼上
中国风 2008-06-11
  • 打赏
  • 举报
回复
--> --> (Roy)生成測試數據

if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([ID] int,[Type] int)
Insert #T
select null,1 union all
select null,1 union all
select null,2 union all
select null,3 union all
select null,1 union all
select null,3 union all
select null,2
Go
declare @1 int,@2 int,@3 int
select @1=50000,@2=60000,@3=70000
update #T
set ID=case when [Type]=1 then @1 when [Type]=2 then @2 when [Type]=3 then @3 end
,@1=case when [Type]=1 then @1+1 else @1 end
,@2=case when [Type]=2 then @2+1 else @2 end
,@3=case when [Type]=3 then @3+1 else @3 end

select * from #T order by Type,ID

ID Type
----------- -----------
50001 1
50002 1
50003 1
60001 2
60002 2
70001 3
70002 3

(7 行受影响)
lff642 2008-06-11
  • 打赏
  • 举报
回复
可以写个函数.传入TYPE.生成最大编号.
sheshuiping 2008-06-10
  • 打赏
  • 举报
回复
可以采用以下方法 :
1. 首先分类别将基础数预先录入一个数据表中.
2. 后期数据的获取以下列方式获取即可。这是其中的一种解法
Create table #t
( types char(1) not null,
ID int not null
)
Insert Into #t
select '1',50001
union all
select '1',50002
union all
select '2',60001
union all
select '2',60002
union all
select '3', 70001
union all
select '3', 70002

declare @types char(1)
set @types='2'
select max(ID)+1 ID From #t

drop table #t
-晴天 2008-06-10
  • 打赏
  • 举报
回复
干什么用?
type,id分别从哪来到哪去?

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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