gpt4 book ai didi

sql - 如何在 Oracle 的循环中重复 Select 语句?

转载 作者:行者123 更新时间:2023-12-04 12:14:57 29 4
gpt4 key购买 nike

我正在阅读很多关于在循环中重复 Select 语句的内容,但是我遇到了一些困难,因为我直到现在还没有找到清楚的东西。我想多次执行一些查询(选择查询),就像在 FOR 循环中一样。任何人都可以帮忙举一些例子吗?

最佳答案

您所询问的基本结构如下所示。
请提供更多信息以获取更具体的代码示例。

DECLARE
l_output NUMBER;
BEGIN
FOR i IN 1..10 LOOP
SELECT 1
INTO l_output
FROM dual;
DBMS_OUTPUT.PUT_LINE('Result: ' || l_output);
END LOOP;
END;

PS:如果需要在SQL*Plus中启用输出,可能需要运行命令
设置服务器输出

更新

要将结果插入到另一个表中:
DECLARE
-- Store the SELECT query in a cursor
CURSOR l_cur IS SELECT SYSDATE DT FROM DUAL;
--Create a variable that will hold each result from the cursor
l_cur_rec l_cur%ROWTYPE;
BEGIN
-- Open the Cursor so that we may retrieve results
OPEN l_cur;
LOOP
-- Get a result from the SELECT query and store it in the variable
FETCH l_cur INTO l_cur_rec;
-- EXIT the loop if there are no more results
EXIT WHEN l_cur%NOTFOUND;
-- INSERT INTO another table that has the same structure as your results
INSERT INTO a_table VALUES l_cur_rec;
END LOOP;
-- Close the cursor to release the memory
CLOSE l_cur;
END;

要创建结果 View ,请参见以下示例:
CREATE VIEW scott.my_view AS 
SELECT * FROM scott.emp;

要使用 View 查看结果:
SELECT * FROM scott.my_view;

关于sql - 如何在 Oracle 的循环中重复 Select 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18083212/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com