Can Sqlplus read the contents of a file into a variable?

前端 未结 3 961
予麋鹿
予麋鹿 2021-01-01 03:05

I\'ve been tinkering with sqlplus for awhile now, and I\'ve managed to get sqlplus to read the contents of a file into a substitution variable as such:

exit          


        
相关标签:
3条回答
  • 2021-01-01 03:37

    Yes, there's a tricky way to do it. Put something into props.txt and run the script:

    DECLARE
      -- the @@ expression must be in separate line as follows
      file_contents VARCHAR2(32767) := '
    @@props.txt
    ';
    BEGIN 
      dbms_output.put_line('===');
      dbms_output.put_line(file_contents);
      dbms_output.put_line('===');
    END;
    /
    

    Note that the file props.txt can not contain an "@" or you'll get nested SQL*PLUS calls

    0 讨论(0)
  • 2021-01-01 03:38

    Please consider using literal quoted string - this allows you to have quotes in the linked file:

    DECLARE
      -- the @@ expression must be in separate line as follows
      file_contents VARCHAR2(32767) := q'[
    @@props.txt
    ]';
    BEGIN 
      dbms_output.put_line('===');
      dbms_output.put_line(file_contents);
      dbms_output.put_line('===');
    END;
    /
    
    0 讨论(0)
  • 2021-01-01 03:42

    No. Load would only store the file contents in Sql*Plus's own sql buffer. You can then run, edit and list the buffer.

    A substitution variable is not the right place to load a file into. Use a bind variable of type clob for that and load the file contents using utl_file. But of course the file has to be located on the server in this case.

    edit: if the data has to be located on the client, your option would be to load the clob using a pl/sql block and several calls to dbms_lob.writeappend

    Your file would have to look like this (cannot test it ATM):

    var l clob;

    begin
      dbms_lob.createtemporary(l);
      dbms_lob.writeappend(l, 'abcdef...');
      dbms_lob.writeappend(l, 'ijkl...');
    end;
    /
    
    0 讨论(0)
提交回复
热议问题