CPName
是一个 VARCHAR;我假设您希望这是一个数字,在这种情况下您只需添加它。
也不需要定义变量a
,它是由 LOOP 隐式声明的。我会称它为i
索引变量的更常见名称。
declare
CPName integer := 1000;
begin
for i in 1 .. 3 loop
insert into clients values (CPName + i, null, null);
end loop;
end;
不过,您可以在一条 SQL 语句中完成所有这些操作;无需使用 PL/SQL。
insert into clients
select 1000 + i, null, null
from dual
cross join ( select level as i
from dual
connect by level <= 3 )
根据您的评论,您实际上想要这样的东西:
insert into clients
with multiply as (
select level - 1 as i
from dual
connect by level <= 3
)
select 1000 + m.i, null, null
from dual
cross join multiply m
cross join multiply
这只有在您想要增加的记录数相同时才有效,所以也许您更愿意这样做,这将为您提供更多的灵活性:
insert into clients
with increments as (
select level - 1 as i
from dual
connect by level <= 5
)
, iterations as (
select level as j
from dual
connect by level <= 3
)
select 1000 + m.i, null, null
from dual
cross join increments m
cross join iterations
使用您的 LOOP 方法,这将涉及第二个内部循环:
declare
CPName integer := 1000;
begin
for i in 1 .. 3 loop
for j in 1 .. 3 loop
insert into clients values (CPName + i, null, null);
end loop;
end loop;
end;