简体   繁体   English

从Shell脚本到SQLPlus的多行变量

[英]Multiple Line Variable into SQLPlus from Shell Script

What is the best way to pass multiple values from one variable into separate records in an oracle db? 将一个变量中的多个值传递到oracle数据库中单独记录中的最佳方法是什么?

I want to take the output from: 我想从输出:

hddlist= ` iostat -Dl|awk '{print ""$1"="$(NF)}' hddlist= ` iostat -Dl|awk '{print ""$1"="$(NF)}' hddlist= iostat -Dl|awk '{print ""$1"="$(NF)}'

This returns output like this: 这将返回如下输出:

hdisk36=0.8
hdisk37=0.8
hdisk38=0.8
hdisk40=5.5   
hdisk52=4.9

I want to insert them into a database like so: 我想像这样将它们插入数据库:

sqlplus -s /nolog <<EOF1
connect / as sysdba
set verify off
insert into my_table ##Single Record Here
EOF1

How can I systematically separate out the values so i can create individual records that look like this: 我如何系统地分离出值,以便创建类似以下的单个记录:

 Disk         Value
---------    -------
hdisk36       0.8
hdisk37       0.8
hdisk38       0.8
hdisk40       5.5   
hdisk52       4.9

I originally tried a while loop with a counter but could not seem to get it to work. 我最初尝试使用计数器进行while循环,但似乎无法使其正常工作。 An exact solution would be nice but some directional advice would be just as helpful. 确切的解决方案会很好,但是一些指导性建议也会有所帮助。

Loop and generate insert statements. 循环并生成插入语句。

sql=$(iostat -Dl | awk '{print ""$1"="$(NF)}' | while IFS== read -r k v ; do
    printf 'insert into mytable (k, v) values (%s, %s);\n' "$k" "$v"
done)

This output can be passed in some manner to sqlplus , perhaps like this 此输出可以某种方式传递给sqlplus ,也许像这样

sqlplus -s /nolog <<EOF1
connect / as sysdba
set verify off
$sql
EOF1

Although, depending on the line format of iostat , it might be simpler to just omit awk and parse with read directly. 虽然,根据iostat的行格式,仅省略awk并直接read解析可能会更简单。

You can redirect the output to a file and then use an external table 您可以将输出重定向到文件,然后使用外部表

It should look something like this: 它看起来应该像这样:

CREATE TABLE hddlist_ext_table (
  disk CHAR(16),
  value CHAR(3)
  ORGANIZATION EXTERNAL (
                         TYPE ORACLE_LOADER DEFAULT DIRECTORY tab_dir
                         ACCESS PARAMETERS (RECORDS DELIMITED BY NEWLINE
                                            FIELDS TERMINATED BY '=')
                         LOCATION ('your_file_name'));

Then you can either use this table for your data or insert-select from it to your table; 然后,您可以使用该表作为数据,也可以从表中进行插入选择。 insert into my_table 插入my_table

select disk, value from hddlist_ext_table;

You can insert multiple rows in a single SQL statement in Oracle like this 您可以像这样在Oracle的单个SQL语句中插入多行

 INSERT ALL
    INTO mytable (column1, column2, column3) VALUES ('val1.1', 'val1.2', 'val1.3')
    INTO mytable (column1, column2, column3) VALUES ('val2.1', 'val2.2', 'val2.3')
    INTO mytable (column1, column2, column3) VALUES ('val3.1', 'val3.2', 'val3.3')
  SELECT * FROM dual;

If you intend to run this script automatically at intervals to then see the results of each disk, you will probably need additional columns to hold the date and time. 如果打算每隔一段时间自动运行此脚本以查看每个磁盘的结果,则可能需要其他列来保存日期和时间。

You might also look at sqlldr as you can specify a control file telling it what your data contains and then this will load the data into a table. 您还可以查看sqlldr,因为您可以指定一个控制文件来告诉它数据包含什么,然后将数据加载到表中。 It is more suited to the purpose if you are loading lots of data than SQL Plus. 如果要加载大量数据,则比SQL Plus更适合此目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM