日期:2014-05-18 浏览次数:20358 次
--原始数据:@T declare @T table(ID varchar(2),NAME varchar(4),TYPE varchar(4),LENGTH int,CitedID varchar(4)) insert @T select '01','S1','INT',1,null union all select '02',null,null,5,'01' union all select '03',null,null,8,'02' while exists (select 1 from @T where NAME is null or TYPE is null or LENGTH is null) update a set a.NAME=isnull(a.name,b.name),a.TYPE=isnull(a.TYPE,b.TYPE),a.LENGTH=isnull(a.LENGTH,b.LENGTH) from @T a join @T b on a.CitedID=b.ID where a.NAME is null or a.TYPE is null or a.LENGTH is null select * from @T /* ID NAME TYPE LENGTH CitedID ---- ---- ---- ----------- ------- 01 S1 INT 1 NULL 02 S1 INT 5 01 03 S1 INT 8 02 */
------解决方案--------------------
declare @T table(ID varchar(2),NAME varchar(4),TYPE varchar(4),LENGTH int,CitedID varchar(4)) insert @T select '01','S1','INT',1,null union all select '02',null,null,5,'01' union all select '03',null,null,8,'02' select * from @T declare @id int SET @ID=03 IF exists(SELECT ID FROM @T WHERE ID=@id AND NAME is NOT null AND TYPE is NOT null AND LENGTH is NOT null) begin SELECT * FROM @T WHERE ID=@id end else begin while exists(select 1 from @T where ID=@id AND (NAME is null or TYPE is null or LENGTH is null)) update a set a.NAME=isnull(a.name,b.name),a.TYPE=isnull(a.TYPE,b.TYPE),a.LENGTH=isnull(a.LENGTH,b.LENGTH) from @T a join @T b on a.CitedID=b.ID where a.NAME is null or a.TYPE is null or a.LENGTH is null select * from @T where ID=@id end /* ID NAME TYPE LENGTH CitedID ---- ---- ---- ----------- ------- 03 S1 INT 8 02 */