27,580
社区成员
发帖
与我相关
我的任务
分享
create table test2(col1 varchar(50),col2 varchar(100))
insert into test2
select '华怡宾馆' , '117.131.106.100华怡宾馆'
union all select NULL , '117.131.106.101华怡宾馆 '
union all select NULL , '117.131.106.102华怡宾馆 '
union all select NULL , '117.131.106.103华怡宾馆 '
union all select NULL , '117.131.106.104华怡宾馆'
union all select '淮海国际广场', '117.131.106.36 淮海国际广场'
union all select '永乐家电' , '117.131.106.67永乐家电'
union all select '东海航运大厦', '117.131.106.83东海航运大厦'
union all select NULL , '117.131.106.84东海航运大厦'
union all select NULL , '117.131.106.85东海航运大厦'
select * from test2
declare @temp1 varchar(100),@temp2 varchar(100),@temp3 varchar(100)
declare cus cursor for
open cus
fetch from cus into @temp1,@temp2
while @@fetch_status=0
begin
if @temp1 is not null
set @temp3=@temp1
if @temp1 is null
update test2 set col1=@temp3 where col2=@temp2
fetch from cus into @temp1,@temp2
end
close cus
deallocate cus
---------------------------------
-- Author: liangCK 小梁
-- Date : 2008-11-20 11:08:33
---------------------------------
--> 生成测试数据: @T
DECLARE @T TABLE (col1 VARCHAR(12),col2 VARCHAR(26))
INSERT INTO @T
SELECT '华怡宾馆','117.131.106.100华怡宾馆' UNION ALL
SELECT null,'117.131.106.101华怡宾馆' UNION ALL
SELECT null,'117.131.106.102华怡宾馆' UNION ALL
SELECT null,'117.131.106.103华怡宾馆' UNION ALL
SELECT null,'117.131.106.104华怡宾馆' UNION ALL
SELECT '淮海国际广场','117.131.106.36淮海国际广场' UNION ALL
SELECT '永乐家电','117.131.106.67永乐家电' UNION ALL
SELECT '东海航运大厦','117.131.106.83东海航运大厦' UNION ALL
SELECT null,'117.131.106.84东海航运大厦' UNION ALL
SELECT null,'117.131.106.85东海航运大厦'
--SQL查询如下:
DECLARE @col1 VARCHAR(20)
UPDATE A SET
@col1=CASE WHEN ISNULL(col1,'')='' THEN @col1 ELSE col1 END,
col1=@col1
FROM @T AS A
SELECT * FROM @T
/*
col1 col2
------------ --------------------------
华怡宾馆 117.131.106.100华怡宾馆
华怡宾馆 117.131.106.101华怡宾馆
华怡宾馆 117.131.106.102华怡宾馆
华怡宾馆 117.131.106.103华怡宾馆
华怡宾馆 117.131.106.104华怡宾馆
淮海国际广场 117.131.106.36淮海国际广场
永乐家电 117.131.106.67永乐家电
东海航运大厦 117.131.106.83东海航运大厦
东海航运大厦 117.131.106.84东海航运大厦
东海航运大厦 117.131.106.85东海航运大厦
(10 行受影响)
*/---------------------------------
-- Author: liangCK 小梁
-- Date : 2008-11-20 11:08:33
---------------------------------
--> 生成测试数据: @T
DECLARE @T TABLE (col1 VARCHAR(12),col2 VARCHAR(26))
INSERT INTO @T
SELECT '华怡宾馆','117.131.106.100华怡宾馆' UNION ALL
SELECT null,'117.131.106.101华怡宾馆' UNION ALL
SELECT null,'117.131.106.102华怡宾馆' UNION ALL
SELECT null,'117.131.106.103华怡宾馆' UNION ALL
SELECT null,'117.131.106.104华怡宾馆' UNION ALL
SELECT '淮海国际广场','117.131.106.36淮海国际广场' UNION ALL
SELECT '永乐家电','117.131.106.67永乐家电' UNION ALL
SELECT '东海航运大厦','117.131.106.83东海航运大厦' UNION ALL
SELECT null,'117.131.106.84东海航运大厦' UNION ALL
SELECT null,'117.131.106.85东海航运大厦'
--SQL查询如下:
DECLARE @col1 VARCHAR(20)
UPDATE A SET
@col1=CASE WHEN ISNULL(col1,'')='' THEN @col1 ELSE col1 END,
col1=@col1
FROM @T AS A
SELECT * FROM @T
/*
col1 col2
------------ --------------------------
华怡宾馆 117.131.106.100华怡宾馆
华怡宾馆 117.131.106.101华怡宾馆
华怡宾馆 117.131.106.102华怡宾馆
华怡宾馆 117.131.106.103华怡宾馆
华怡宾馆 117.131.106.104华怡宾馆
淮海国际广场 117.131.106.36淮海国际广场
永乐家电 117.131.106.67永乐家电
东海航运大厦 117.131.106.83东海航运大厦
东海航运大厦 117.131.106.84东海航运大厦
东海航运大厦 117.131.106.85东海航运大厦
(10 行受影响)
*/