是否可以这样做来添加真实的产品记录
是的,只需使用真实数据调用您的过程 1000 次即可。
例如:
begin
InsertProduct('Apple',45,'A red fruit');
InsertProduct('Banana',33,'A yellow fruit');
InsertProduct('Cucumber',70,'A green vegetable');
InsertProduct('Date',5,'A brown fruit');
commit;
end;
,
您是否考虑过使用 FORALL
?如果您能够为要插入的数据创建记录集合,则可以使用类似于以下示例的语句将它们全部插入。
使用 FORALL
还会使您的代码执行得更快,因为它只有 1 个 INSERT
语句,其中包含所有记录,而 1000 个单独的 INSERT
语句。
DECLARE
TYPE product_t IS TABLE OF product%ROWTYPE;
l_products product_t := product_t();
BEGIN
l_products.extend(3); --number of items we'll be creating
l_products (1).name := 'name1';
l_products (1).price := 1;
l_products (1).description := 'description1';
l_products (2).name := 'name2';
l_products (2).price := 2;
l_products (2).description := 'description2';
l_products (3).name := 'name3';
l_products (3).price := 3;
l_products (3).description := 'description3';
FORALL i IN 1 .. l_products.COUNT
INSERT INTO product (name,price,description,create_date)
VALUES (l_products (i).name,l_products (i).price,l_products (i).description,SYSDATE);
END;
如果您想了解如何创建示例数据,下面的示例将创建 1500 行示例数据。同样,这将比您提供的代码执行得更快,因为使用您的代码需要 1 INSERT
和 1500 INSERT
秒。
DECLARE
TYPE product_t IS TABLE OF product%ROWTYPE;
l_products product_t := product_t ();
BEGIN
FOR i IN 1 .. 1500
LOOP
l_products.EXTEND;
l_products (i).name := DBMS_RANDOM.string ('x',10);
l_products (i).price := DBMS_RANDOM.VALUE (low => 1,high => 100000);
l_products (i).description := DBMS_RANDOM.string ('x',10);
END LOOP;
FORALL i IN 1 .. l_products.COUNT
INSERT INTO product (name,SYSDATE);
END;
本文链接:https://www.f2er.com/10099.html