You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否使用Apex Data Export将导出文件存储为表中BLOB?

Can Apex Data Export be used to store export results as BLOB in a table?

Yes, you absolutely can use Apex Data Export to generate files (like PDF) and store them as BLOBs in your database table. The error you're seeing (PLS-00382: expression is of wrong type) comes from a type mismatch between the variable you're using and what the UPDATE statement expects.

Root Cause

You declared l_export as apex_data_export.t_export, but the file_blob column is a BLOB type. The apex_data_export.export function returns a t_export object by default, not a raw BLOB. To get a BLOB directly, you need to use the correct overload of the export function that specifies the output type as BLOB.

Corrected Code

Here's the fixed version of your code that properly generates a BLOB and stores it in your table:

DECLARE
    l_context apex_exec.t_context; 
    l_export  BLOB; -- Changed type to BLOB
BEGIN
    -- Open query context
    l_context := apex_exec.open_query_context(
        p_location    => apex_exec.c_location_local_db,
        p_sql_query   => 'select * from emp' );

    -- Export query result as PDF BLOB
    l_export := apex_data_export.export (
        p_context   => l_context,
        p_format    => apex_data_export.c_format_pdf,
        p_output    => apex_data_export.c_output_blob -- Specify output as BLOB
    );

    -- Close the context (always do this after use)
    apex_exec.close( l_context );

    -- Update the table with the BLOB
    UPDATE my_table
    SET file_blob = l_export;
    
    -- Commit if needed (depends on your transaction context)
    -- COMMIT;

EXCEPTION
    WHEN others THEN
        -- Ensure context is closed even on error
        IF apex_exec.is_open(l_context) THEN
            apex_exec.close( l_context );
        END IF;
        raise;
END;
/

Key Changes:

  • Changed l_export type from apex_data_export.t_export to BLOB
  • Added p_output => apex_data_export.c_output_blob parameter to apex_data_export.export to explicitly request a BLOB output
  • Added a check IF apex_exec.is_open(l_context) in the exception block to avoid trying to close an already closed context

Additional Notes:

  • If you need to store other metadata (like filename, MIME type), you can retrieve those by using the t_export object approach (access properties like filename or mime_type), but for direct BLOB storage, the above method is simpler.
  • Remember to commit the transaction if your environment doesn't auto-commit (e.g., in a PL/SQL block run via SQL Developer or Apex SQL Workshop).

内容的提问来源于stack exchange,提问作者tomato

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 09:07:19