DB2 9.7: 在 DB2 9.7 中使用 PL/SQL 匿名块
2009-10-16 00:00:00 来源:WEB开发网使用匿名块模拟应用程序运行
如前所述,匿名块的最常见用法就是调用过程语言对象,通常是为了进行测试。清单 4 显示了如何通过 PL/SQL 匿名块的帮助模拟应用程序的运行。清单 4 的代码在模拟应用程序运行的同时捕获性能度量指标。该匿名块模拟为来自现有 CUSTOMER 表的随机客户创建 10 个随机订单。它还输出测试的开始和终止时间,以及每次运行的订单细节。您可以轻松地将订单数改为从 10 到 20,然后重新运行这个匿名块而不需要再进行编译。您还可以为测试添加更多的性能度量指标。
清单 4. 在 PL/SQL 匿名块的帮助下模拟应用程序的运行
SET SERVEROUTPUT ON
/
DECLARE
v_customer_id customer.customer_id%TYPE;
product_id product.product_id%TYPE:=1;
o_order_id orders.order_id%TYPE;
v_test_start TIMESTAMP;
BEGIN
SELECT CURRENT TIMESTAMP INTO v_test_start FROM dual;
FOR k IN 1..10 LOOP
SELECT customer_id INTO v_customer_id FROM customer ORDER BY RAND() FETCH FIRST 1
ROW ONLY;
FOR i IN (
SELECT product_id, CAST(RAND()*50 as integer)+1 as quantity
FROM product
WHERE ROWNUM < CAST(RAND()*10 as integer))
LOOP
add_item_to_shopping_cart(i.product_id, i.quantity);
END LOOP;
create_order(v_customer_id, o_order_id);
DBMS_OUTPUT.PUT_LINE('--------------------------------------------');
END LOOP;
DBMS_OUTPUT.PUT_LINE('Test start: ' || v_test_start);
DBMS_OUTPUT.PUT_LINE('Test end : ' || CURRENT TIMESTAMP);
END;
/
Output:
Customer : Mike, Smith
Order creation : 07-06-2009
Estimated Delivery : 07-09-2009
Status : Shipped
Total price : $ 150,615.44
--------------------------------------------
Customer : Joan, Jett
Order creation : 07-06-2009
Estimated Delivery : 07-09-2009
Status : Shipped
Total price : $ 159,445.77
...
...
...
Customer : Colin, Taylor
Order creation : 07-06-2009
Estimated Delivery : 07-09-2009
Status : Shipped
Total price : $ 266,242.78
--------------------------------------------
Test start: 2009-07-06-11.10.11.500000
Test end : 2009-07-06-11.10.11.546000
更多精彩
赞助商链接