Wednesday, 13 November 2024

Oracle PLSQL Script - How to Generate File and Place it on Oracle File Server Using

 DECLARE

  -- Declare file handler
  file_handle UTL_FILE.FILE_TYPE;

  -- Declare variables for data to be written
  v_line VARCHAR2(100);
  v_filename VARCHAR2(50) := 'handlingunits'||TO_CHAR(SYSDATE,'MMDDRRHH24MISS')||'.csv';  -- Name of the file
  v_dir VARCHAR2(50) := 'XX_CAO_OUTBOUND';  -- Directory path (ensure it's accessible by Oracle)

 

BEGIN
  -- Open the file for writing. If the file does not exist, it will be created.
  file_handle := UTL_FILE.FOPEN(v_dir, v_filename, 'w');  -- 'w' mode means write

  -- Example data to write to the file
  FOR xxtlm_rec IN (SELECT DISTINCT ';;;'||item||';'||vendor_lot||';1;1;1;' line
                    FROM xxtlm.xxtlm_rcv_transactions xrt
                    WHERE xrt.organization_id = (SELECT organization_id FROM mtl_parameters WHERE organization_code = 'TIF')
                        AND xrt.status_id = 2
                        AND transaction_type = 'RECEIVE'
                    UNION
                    SELECT '99999999;99999999;99999999;99999999;99999999;99999999;99999999;99999999;99999999;99999999'
                    FROM dual
                    ORDER BY 1 DESC) 
    LOOP    
    -- Write the data to the file
    UTL_FILE.PUT_LINE(file_handle, xxtlm_rec.line);
  END LOOP;

  -- Close the file after writing
  UTL_FILE.FCLOSE(file_handle);

 

  -- Success message
  DBMS_OUTPUT.PUT_LINE('Data written successfully to ' || v_dir || '/' || v_filename);

 

EXCEPTION
  WHEN OTHERS THEN
    -- Handle any exceptions
    DBMS_OUTPUT.PUT_LINE('Exception: '||SQLERRM);
    IF UTL_FILE.IS_OPEN(file_handle) THEN
      UTL_FILE.FCLOSE(file_handle);
    END IF;
    DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
/

6 comments:

  1. "Great post! I was looking for a detailed guide on how to generate files using Oracle PLSQL and place them on the file server. This is exactly what I needed. Thanks for sharing!"
    SEO Company in Delhi
    Invest in Brands

    ReplyDelete
  2. "This tutorial is extremely helpful! You explained each step clearly, making it easy to follow along. Keep up the good work!"
    Rack Supported Mezzanine floor
    Mezzanine floor manufacturer

    ReplyDelete
  3. "I tried your script, and it worked perfectly! It saved me a lot of time on a recent project. Thank you for such a comprehensive post!"
    Office Furniture Manufacturer Delhi
    Cantilever racking system

    ReplyDelete
  4. "Thank you for explaining the process so clearly! I’ve struggled to find reliable resources on Oracle file generation, but this post covers everything in one place."
    SEO Company in India
    Franchise

    ReplyDelete
  5. "I’m new to Oracle PLSQL, and this guide was incredibly helpful. Looking forward to more of your posts on Oracle and database management!"
    Mezzanine floor
    Mezzanine floor in India

    ReplyDelete
  6. "Could you please elaborate on file permissions when placing files on Oracle File Server? I encountered some permission issues that stopped my script."
    Office Furniture Manufacturer Noida
    cantilever storage rack

    ReplyDelete