SQL问题..精彩

来源:百度知道 编辑:UC知道 时间:2024/07/05 12:25:17
SQL中怎么样操作可以让下面行字段复制上面行字段
如:
1.AA 1111
2.NULL 1111
3.NULL 2
4.BB 2222
5.NULL 11
6.NULL 32
7.NULL 33
怎么用命令实现阶段一行下面为空的字段拷贝成AA
5,6,7行的字段拷贝为BB
第二列的数据不变

假设三个字段名为id,name,num,表名为tablename
则这样:
update tablename
set name=
(select top 1 name from tablename b where b.id<tablename.id and name is not null order by id)
where name is null
==================
对不起,答案中带TOP 1的子句忘记了排序 order by id,现在加上

看不清楚你说的问题;
不过可以给你个函数用下
sql server 中isnull(字段,'代替的字符')
你可以这样做:
select isnull(阶段一,'AA') as 别名 from table

视图

不明白。。。

看完关于游标的教程你就会了 - -#