gpt4 book ai didi

sql - 插入查询后的行数和选择查询的行数

转载 作者:行者123 更新时间:2023-12-05 06:44:43 28 4
gpt4 key购买 nike

我正在向表中插入记录,并使用选择查询获取值。

insert into my_table (....) 
select a.name, b.age, c.id, d.address
from table1 a, table2 b, table3 c, table4 d
where a.age=23 and d.addredd like '%street%';

我想比较选择查询的行数和 my_table 的行数(插入后)。

如何在不花费更多处理时间的情况下获取两者的行数。

谢谢

最佳答案

要获取插入行的总数,您可以使用 SQL%ROWCOUNT

DECLARE

BEGIN
insert into my_table (....)
select a.name, b.age, c.id, d.address
from table1 a, table2 b, table3 c, table4 d
where a.age=23 and d.addredd like '%street%';

DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT);
END;

编辑:

另一种方法是创建一个返回该值的函数:

CREATE OR REPLACE FUNCTION get_count_inserted
RETURN NUMBER
IS
PRAGMA AUTONOMOUS_TRANSACTION;
RESULT NUMBER;
BEGIN
insert into my_table (....) -- Your query
select a.name, b.age, c.id, d.address
from table1 a, table2 b, table3 c, table4 d
where a.age=23 and d.addredd like '%street%';

RESULT := SQL%ROWCOUNT; --getting the count

COMMIT;

RETURN RESULT; --returning result
END;

创建函数后,您可以像这样查询它:

SELECT get_count_inserted FROM DUAL; --this will return total of rows inserted

关于sql - 插入查询后的行数和选择查询的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27923897/

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