Pass values read from a file as input to an SQL query in Oracle

前端 未结 2 891
囚心锁ツ
囚心锁ツ 2021-01-17 05:31
#cat file.txt
 12354
 13456
 13498

#!/bin/bash
for i in `cat file.txt`
do
    sqlplus XXXXX/XXXXX@DB_NAME << EOF 
        selec         


        
2条回答
  •  生来不讨喜
    2021-01-17 05:56

    Here is the right way to use the heredoc <<, along with the choice of while read instead of for to read the file:

    #!/bin/bash
    
    while read -r value; do
      sqlplus xxxxx/xxxxx@db_name << EOF
        select * from table_name where id='$value';
        # make sure heredoc marker "EOF" is not indented
    EOF
    done < file.txt
    

    See also:

    • How can I write a here doc to a file in Bash script?

    • BashFAQ001 to understand why for loop is not the best way to read text lines from a file.

提交回复
热议问题