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
使用你的循環方法,此將涉及第二個內部循環:
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;
來源
2013-07-27 12:00:25
Ben
以這種方式如何增加oracle中的varchar類型變量? – selvackp
你不能「遞增」一個VARCHAR變量;這個概念不存在。但是,如果您使用ASCII()函數,那麼您可以使用相同的方法。 – Ben
是的,好吧,本先生.. – selvackp