如何在数据表用前行的数据替换当前行的数据,请各位大大们指点

laiqun123 2008-11-20 11:07:20
表格式如下:
col1 col2
华怡宾馆 117.131.106.100华怡宾馆
NULL 117.131.106.101华怡宾馆
NULL 117.131.106.102华怡宾馆
NULL 117.131.106.103华怡宾馆
NULL 117.131.106.104华怡宾馆
淮海国际广场 117.131.106.36 淮海国际广场
永乐家电 117.131.106.67永乐家电
东海航运大厦 117.131.106.83东海航运大厦
NULL 117.131.106.84东海航运大厦
NULL 117.131.106.85东海航运大厦
要得到结果如下:
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东海航运大厦

这代码应该怎么写,小弟刚学SQL,底子不行,还请各位帮忙
...全文
118 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
laiqun123 2008-11-23
  • 打赏
  • 举报
回复
多谢各位了
chenyifei211 2008-11-20
  • 打赏
  • 举报
回复
不过还是楼上的方法简答一些
chenyifei211 2008-11-20
  • 打赏
  • 举报
回复
用游标来写

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

liangCK 2008-11-20
  • 打赏
  • 举报
回复
---------------------------------
-- 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 行受影响)
*/
liangCK 2008-11-20
  • 打赏
  • 举报
回复
---------------------------------
-- 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 行受影响)
*/

27,580

社区成员

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

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