Oracle 在SHELL中迭代PL/SQL结果
我想在SHELL脚本中迭代PL/SQL行,并且对于每一行,我想使用当前行执行一些代码。在这一点上,我得到:Oracle 在SHELL中迭代PL/SQL结果,oracle,shell,unix,Oracle,Shell,Unix,我想在SHELL脚本中迭代PL/SQL行,并且对于每一行,我想使用当前行执行一些代码。在这一点上,我得到: VALUE='sqlplus -s /nolog <<EOF CONNECT ${CONNECT} select smth from table; / EXIT EOF' for i in "${VALUE[@]}" do ##some code using "i" variable done
VALUE='sqlplus -s /nolog <<EOF
CONNECT ${CONNECT}
select smth from table;
/
EXIT
EOF'
for i in "${VALUE[@]}"
do
##some code using "i" variable
done
VALUE='sqlplus-s/nolog您可以按如下方式迭代结果集:
SQL> select car_model from available_models
2 group by car_model ;
CAR_MODEL
------------------------------
Corsair
Executive
Country Squire
SQL>
[oracle@ora12c 1]$ cat test.sh
CONNECT='z_test/welcome1'
VALUE=`sqlplus -s /nolog <<EOF
CONNECT ${CONNECT}
set head off
select car_model from available_models
group by car_model
/
EXIT
EOF`
echo "resultset: "
echo "${VALUE}"
echo " "
echo "now iterate ..."
let rec=0
echo "${VALUE}" |while read line
do
echo "processing $rec: $line"
let rec=rec+1
done
[oracle@ora12c 1]$
重写shell脚本(使用WHILE),如下所示:
SQL> select car_model from available_models
2 group by car_model ;
CAR_MODEL
------------------------------
Corsair
Executive
Country Squire
SQL>
[oracle@ora12c 1]$ cat test.sh
CONNECT='z_test/welcome1'
VALUE=`sqlplus -s /nolog <<EOF
CONNECT ${CONNECT}
set head off
select car_model from available_models
group by car_model
/
EXIT
EOF`
echo "resultset: "
echo "${VALUE}"
echo " "
echo "now iterate ..."
let rec=0
echo "${VALUE}" |while read line
do
echo "processing $rec: $line"
let rec=rec+1
done
[oracle@ora12c 1]$
请注意,第#0行为空,应为空,因为它也是结果集的一部分
添加“设置页面大小0”将删除此空行:
[oracle@ora12c 1]$ cat test.sh
CONNECT='z_test/welcome1'
VALUE=`sqlplus -s /nolog <<EOF
CONNECT ${CONNECT}
set head off
set pagesize 0
select car_model from available_models
group by car_model
/
EXIT
EOF`
......
关于您可以按如下方式迭代您的结果集:
SQL> select car_model from available_models
2 group by car_model ;
CAR_MODEL
------------------------------
Corsair
Executive
Country Squire
SQL>
[oracle@ora12c 1]$ cat test.sh
CONNECT='z_test/welcome1'
VALUE=`sqlplus -s /nolog <<EOF
CONNECT ${CONNECT}
set head off
select car_model from available_models
group by car_model
/
EXIT
EOF`
echo "resultset: "
echo "${VALUE}"
echo " "
echo "now iterate ..."
let rec=0
echo "${VALUE}" |while read line
do
echo "processing $rec: $line"
let rec=rec+1
done
[oracle@ora12c 1]$
重写shell脚本(使用WHILE),如下所示:
SQL> select car_model from available_models
2 group by car_model ;
CAR_MODEL
------------------------------
Corsair
Executive
Country Squire
SQL>
[oracle@ora12c 1]$ cat test.sh
CONNECT='z_test/welcome1'
VALUE=`sqlplus -s /nolog <<EOF
CONNECT ${CONNECT}
set head off
select car_model from available_models
group by car_model
/
EXIT
EOF`
echo "resultset: "
echo "${VALUE}"
echo " "
echo "now iterate ..."
let rec=0
echo "${VALUE}" |while read line
do
echo "processing $rec: $line"
let rec=rec+1
done
[oracle@ora12c 1]$
请注意,第#0行为空,应为空,因为它也是结果集的一部分
添加“设置页面大小0”将删除此空行:
[oracle@ora12c 1]$ cat test.sh
CONNECT='z_test/welcome1'
VALUE=`sqlplus -s /nolog <<EOF
CONNECT ${CONNECT}
set head off
set pagesize 0
select car_model from available_models
group by car_model
/
EXIT
EOF`
......
关于您可以按如下方式迭代您的结果集:
SQL> select car_model from available_models
2 group by car_model ;
CAR_MODEL
------------------------------
Corsair
Executive
Country Squire
SQL>
[oracle@ora12c 1]$ cat test.sh
CONNECT='z_test/welcome1'
VALUE=`sqlplus -s /nolog <<EOF
CONNECT ${CONNECT}
set head off
select car_model from available_models
group by car_model
/
EXIT
EOF`
echo "resultset: "
echo "${VALUE}"
echo " "
echo "now iterate ..."
let rec=0
echo "${VALUE}" |while read line
do
echo "processing $rec: $line"
let rec=rec+1
done
[oracle@ora12c 1]$
重写shell脚本(使用WHILE),如下所示:
SQL> select car_model from available_models
2 group by car_model ;
CAR_MODEL
------------------------------
Corsair
Executive
Country Squire
SQL>
[oracle@ora12c 1]$ cat test.sh
CONNECT='z_test/welcome1'
VALUE=`sqlplus -s /nolog <<EOF
CONNECT ${CONNECT}
set head off
select car_model from available_models
group by car_model
/
EXIT
EOF`
echo "resultset: "
echo "${VALUE}"
echo " "
echo "now iterate ..."
let rec=0
echo "${VALUE}" |while read line
do
echo "processing $rec: $line"
let rec=rec+1
done
[oracle@ora12c 1]$
请注意,第#0行为空,应为空,因为它也是结果集的一部分
添加“设置页面大小0”将删除此空行:
[oracle@ora12c 1]$ cat test.sh
CONNECT='z_test/welcome1'
VALUE=`sqlplus -s /nolog <<EOF
CONNECT ${CONNECT}
set head off
set pagesize 0
select car_model from available_models
group by car_model
/
EXIT
EOF`
......
关于您可以按如下方式迭代您的结果集:
SQL> select car_model from available_models
2 group by car_model ;
CAR_MODEL
------------------------------
Corsair
Executive
Country Squire
SQL>
[oracle@ora12c 1]$ cat test.sh
CONNECT='z_test/welcome1'
VALUE=`sqlplus -s /nolog <<EOF
CONNECT ${CONNECT}
set head off
select car_model from available_models
group by car_model
/
EXIT
EOF`
echo "resultset: "
echo "${VALUE}"
echo " "
echo "now iterate ..."
let rec=0
echo "${VALUE}" |while read line
do
echo "processing $rec: $line"
let rec=rec+1
done
[oracle@ora12c 1]$
重写shell脚本(使用WHILE),如下所示:
SQL> select car_model from available_models
2 group by car_model ;
CAR_MODEL
------------------------------
Corsair
Executive
Country Squire
SQL>
[oracle@ora12c 1]$ cat test.sh
CONNECT='z_test/welcome1'
VALUE=`sqlplus -s /nolog <<EOF
CONNECT ${CONNECT}
set head off
select car_model from available_models
group by car_model
/
EXIT
EOF`
echo "resultset: "
echo "${VALUE}"
echo " "
echo "now iterate ..."
let rec=0
echo "${VALUE}" |while read line
do
echo "processing $rec: $line"
let rec=rec+1
done
[oracle@ora12c 1]$
请注意,第#0行为空,应为空,因为它也是结果集的一部分
添加“设置页面大小0”将删除此空行:
[oracle@ora12c 1]$ cat test.sh
CONNECT='z_test/welcome1'
VALUE=`sqlplus -s /nolog <<EOF
CONNECT ${CONNECT}
set head off
set pagesize 0
select car_model from available_models
group by car_model
/
EXIT
EOF`
......
关于要么查询没有返回数据,要么for循环中出现错误;在这种情况下,您可以相应地进行更新;在这种情况下,您可以相应地进行更新;在这种情况下,您可以相应地进行更新;在这种情况下,您可以相应地进行更新。