将值从特定行复制到另一行

时间:2019-02-10 13:01:40

标签: sql

我想检查row2的值是否为空,然后将同一记录的row1的值复制到row2

我使用while循环检查是否为空,然后复制值

Declare @val varchar(255) 
Select @val = row2 from student While(@val IS NULL) 
Begin
Update student Set row2 = (select row1 from student) 
End
 Go

有子查询错误不能使用=,<,>

2 个答案:

答案 0 :(得分:1)

您想要做这样的事情:

update student
    set row2 = row1
    where row2 is null ;

您的问题非常令人困惑,因为row1row2是列的非常奇怪的名称。

答案 1 :(得分:0)

您可以使用test.jsp。这样,如果coalesce的值为null,则处理第二个参数(row2)。顺便说一下,使用row1col1代替2row 1更适合列名,

2

要申请整个数据集,应删除过滤器Update student Set row2 = coalesce(row2,row1) Where id = @sid -- individual student ID