补入编号

xucome 2008-05-24 01:24:43
原始表
iID cCode Text
3 0001 套件
4 组件
7 组件
8 0002 套件
9 组件
19 0003 套件
34 组件
54 组件


目的表: (把Text为组件的cCode写入它上面的套件单号)
iID cCode Text
3 0001 套件
4 0001 组件
7 0001 组件
8 0002 套件
9 0002 组件
19 0003 套件
34 0003 组件
54 0003 组件
...全文
58 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
pt1314917 2008-05-24
  • 打赏
  • 举报
回复

declare @tb table (iID int, cCode nvarchar(5),cText nvarchar(10))
insert into @tb select 3,'0001','套件'
union all select 4 ,null,'组件'
union all select 7, null,'组件'
union all select 8, '0002', '套件'
union all select 9,null,'组件'
union all select 19, '0003', '套件'
union all select 34,null,'组件'
union all select 54,null,'组件'



update @tb set ccode=(select ccode from @tb where iid=(select max(iid) from @tb where iID<a.iID and ccode is not null))
from @tb a where a.ccode is null

select * from @tb
utpcb 2008-05-24
  • 打赏
  • 举报
回复
declare @tb table (iID int, cCode nvarchar(5),cText nvarchar(10)) insert into @tb select 3,'0001','套件' union all select 4 ,null,'组件' union all select 7, null,'组件' union all select 8, '0002', '套件' union all select 9,null,'组件' union all select 19, '0003', '套件' union all select 34,null,'组件' union all select 54,null,'组件' --select * from @tb select iID, case when cCode is not null then cCode else ( select max(cCode) from @tb where cText ='套件' and iID < t.iID and cText <> t.cText ) end as cCode, cText from @tb t /* (8 row(s) affected) iID cCode cText ----------- ----- ---------- 3 0001 套件 4 0001 组件 7 0001 组件 8 0002 套件 9 0002 组件 19 0003 套件 34 0003 组件 54 0003 组件 (8 row(s) affected) */

hery2002 2008-05-24
  • 打赏
  • 举报
回复
declare @tb table (iID int, cCode nvarchar(5),cText nvarchar(10))
insert into @tb select 3,'0001','套件'
union all select 4 ,null,'组件'
union all select 7, null,'组件'
union all select 8, '0002', '套件'
union all select 9,null,'组件'
union all select 19, '0003', '套件'
union all select 34,null,'组件'
union all select 54,null,'组件'

--select * from @tb

select iID,
case when cCode is not null then cCode else ( select max(cCode) from @tb where cText ='套件' and iID < t.iID and cText <> t.cText ) end as cCode,
cText from @tb t
/*
(8 row(s) affected)
iID cCode cText
----------- ----- ----------
3 0001 套件
4 0001 组件
7 0001 组件
8 0002 套件
9 0002 组件
19 0003 套件
34 0003 组件
54 0003 组件

(8 row(s) affected)
*/
xucome 2008-05-24
  • 打赏
  • 举报
回复
原始表
iID cCode Text
3 0001 套件
4 组件
7 组件
8 0002 套件
9 组件
19 0003 套件
34 组件
54 组件


目的表: (把Text为组件的cCode写入它上面的套件单号)
iID cCode Text
3 0001 套件
4 0001 组件
7 0001 组件
8 0002 套件
9 0002 组件
19 0003 套件
34 0003 组件
54 0003 组件

27,579

社区成员

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

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