Oracle
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Oracle

Mở con trỏ cho tên bảng động trong thủ tục PL / SQL

Tôi sẽ làm điều này như một câu lệnh insert-as-select duy nhất, phức tạp chỉ bởi thực tế là bạn đang chuyển vào table_name, vì vậy chúng tôi cần sử dụng sql động.

Tôi sẽ làm điều đó giống như:

CREATE OR REPLACE PROCEDURE some_name(p_table_name IN VARCHAR2,
                                      p_chunk_size IN NUMBER,
                                      p_row_limit  IN NUMBER) AS

  v_table_name VARCHAR2(32); -- 30 characters for the tablename, 2 for doublequotes in case of case sensitive names, e.g. "table_name"

  v_insert_sql CLOB;
BEGIN
  -- Sanitise the passed in table_name, to ensure it meets the rules for being an identifier name. This is to avoid SQL injection in the dynamic SQL
  -- statement we'll be using later.
  v_table_name := DBMS_ASSERT.ENQUOTE_LITERAL(p_table_name);

  v_insert_sql := 'insert into chunks (common_column_name, chunk_number)'||CHR(10)|| -- replace the column names with the actual names of your chunks table columns.
                  'select common_column,'||CHR(10)||
                  '       ora_hash(substr(common_column, 1, 15), :p_chunk_size) AS chunk_number'||CHR(10)||
                  'from   '||v_table_name||CHR(10)||
                  'where  rownum <= :p_row_limit';

  -- Used for debug purposes, so you can see the definition of the statement that's going to be run.
  -- Remove before putting the code in production / convert to proper logging code:
  dbms_output.put_line(v_insert_sql);

  -- Now run the statement:
  EXECUTE IMMEDIATE v_insert_sql USING p_chunk_size, p_row_limit;

  -- I've included the p_row_limit in the above statement, since I'm not sure if your original code loops through all the rows once it processes the
  -- first p_row_limit rows. If you need to insert all rows from the p_table_name into the chunks table, remove the predicate from the insert sql and the extra bind variable passed into the execute immediate.
END some_name;
/

Bằng cách sử dụng một câu lệnh insert-as-select, bạn đang sử dụng cách hiệu quả nhất để thực hiện công việc. Thực hiện thu thập hàng loạt (mà bạn đang sử dụng) sẽ sử dụng hết bộ nhớ (lưu trữ dữ liệu trong mảng) và gây ra chuyển đổi ngữ cảnh bổ sung giữa các công cụ PL / SQL và SQL mà câu lệnh insert-as-select tránh được.




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Oracle Joins (Sql Joins) là gì?

  2. Từ đồng nghĩa công khai so với mẫu schema.object

  3. Làm cách nào để xử lý dấu ngoặc kép 'trong SQL

  4. Sau khi thả phân vùng, chỉ mục trở nên không sử dụng được, tôi phải làm gì,

  5. Kết nối nhà phát triển SQL với Oracle 12c