从 table1 我想从某些列收集值。首先,我尝试将一张表复制到另一张表,但在尝试时卡住了:
for row in row_count
for column in column_count
insert into table2 at (x,y) value from (row,column)
column++
end
row++
end
Run Code Online (Sandbox Code Playgroud)
我计算行数的第一个函数是:
create or replace FUNCTION func_count_rows(table_name IN varchar2,
debug boolean default false)
RETURN number IS
total number(2) := 0;
BEGIN
IF debug = true THEN
DBMS_OUTPUT.put('Function count rows: ');
DBMS_OUTPUT.PUT_LINE('select count(*) from ' || table_name || ';');
DBMS_OUTPUT.put('Returns: ');
DBMS_OUTPUT.PUT_LINE('');
END IF;
execute immediate 'select count(*) from ' || table_name into total;
RETURN total;
END;
Run Code Online (Sandbox Code Playgroud)
然后我的程序首先打印值,但我卡在这里:
create or replace procedure gather_values (rows_quantity in VARCHAR2,
column_count in VARCHAR2,
debug boolean default false
)
is begin
select
FOR i IN 1..rows_quantity LOOP
DBMS_OUTPUT.PUT_LINE('#### ROW 1 ####');
FOR i IN 1..94 LOOP
END LOOP;
END LOOP;
end;
Run Code Online (Sandbox Code Playgroud)
我不知道如何从表的精确 (x,y) 中获取列数量和值。
你能帮我吗?谢谢你。
我忘了告诉我我正在使用 oracle SQL 环境。
首先,这与 PL/SQL 没有任何共同之处:
for row in row_count
for column in column_count
insert into table2 at (x,y) value from (row,column)
column++
end
row++
end
Run Code Online (Sandbox Code Playgroud)
请参阅此处的文档。
要将一个表中的所有行复制到另一个表:
insert into table2 (x,y)
select a, b
from table1;
Run Code Online (Sandbox Code Playgroud)
它是一个简单的 SQL 查询,可以按原样使用,也可以在 PL/SQL 过程中使用。
迭代表的所有行有很多可能性。最简单的:
for i in (select column1, column2, ... from table1) loop
dbms_output.put_line(i.column1);
end loop;
Run Code Online (Sandbox Code Playgroud)
另一种方式:
要计算表中的行数,您可以使用 SQL 查询:
select count(*)
from table1
Run Code Online (Sandbox Code Playgroud)
或几乎相同的 PL/SQL 代码(您不需要使用execute immediate):
declare
total number;
begin
select count(*)
into total
from table1;
dbms_output.put_line('count of rows: ' || total);
end;
/
Run Code Online (Sandbox Code Playgroud)
但在任何情况下,您都不需要知道一个表包含多少行和列来迭代它们。您只需要知道如何过滤,您想要迭代其中的哪些。
| 归档时间: |
|
| 查看次数: |
26868 次 |
| 最近记录: |