PL/SQL 在更新中使用 IF 语句
PL/SQL using IF statement inside Update
想知道是否有这样的事情:
begin
for C2 in cursor_sal loop
if something then
update emp
set name = George
where ID = 1
elsif something2 then
update emp
set name = Steve
where ID = 4
end if
end loop;
end;
可以变成这样或类似的东西:
begin
for C2 in cursor_sal loop
update emp
if something then
set name = George
elsif something2 then
set name = Steve
end if
where current of C2
end loop;
end;
或者这是不可能的,我坚持第一个例子?
你可以像这样在更新语句中使用 case 语句
update emp
set name = case when something then 'George'
when something2 then 'Steve'
end;
此外,如果条件相同,您也可以使用解码功能。
update 是 sql 语句,if 语句是 pl/sql 构造。您可以在 pl/sql 中使用 sql 语句,但不能在 sql.
中使用 pl/sql 构造
据我所知,执行此操作的最佳方法是按照以下示例使用 case 语句。代码未经测试,但应该足以让您继续。
begin
for C2 in cursor_sal loop
update emp
set name = case
when something then 'George'
when somethingelse then 'something2'
else 'somthing 3'
end
where current of C2
end loop;
end;
想知道是否有这样的事情:
begin
for C2 in cursor_sal loop
if something then
update emp
set name = George
where ID = 1
elsif something2 then
update emp
set name = Steve
where ID = 4
end if
end loop;
end;
可以变成这样或类似的东西:
begin
for C2 in cursor_sal loop
update emp
if something then
set name = George
elsif something2 then
set name = Steve
end if
where current of C2
end loop;
end;
或者这是不可能的,我坚持第一个例子?
你可以像这样在更新语句中使用 case 语句
update emp
set name = case when something then 'George'
when something2 then 'Steve'
end;
此外,如果条件相同,您也可以使用解码功能。
update 是 sql 语句,if 语句是 pl/sql 构造。您可以在 pl/sql 中使用 sql 语句,但不能在 sql.
中使用 pl/sql 构造据我所知,执行此操作的最佳方法是按照以下示例使用 case 语句。代码未经测试,但应该足以让您继续。
begin
for C2 in cursor_sal loop
update emp
set name = case
when something then 'George'
when somethingelse then 'something2'
else 'somthing 3'
end
where current of C2
end loop;
end;