sql - 将 SQL 结果传递到 shell 脚本中的数组时标识符无效

标签 sql linux oracle shell sqlplus

我正在尝试将 SQL 查询结果存储到 shell 脚本中的数组中,但在运行 .sh 时遇到无效标识符错误

你能检查一下我的代码中有什么问题吗?

#!/usr/bin/ksh

echo Start Executing SQL commands

array=$(sqlplus -s apps/apps << eof
SET PAGESIZE 0;
SELECT directory_name from all_directories where directory_name like '%XXBP%';
eof)


printf '%s\n' "${array[@]}"

这是我得到的错误:

Screenshot of the error i get

我知道问题出在我的运算符(operator) % 上,但我需要它来限制我的查询结果。

最佳答案

这听起来像 this very specific ksh bug其中 here-documents 默默地将单引号转换为双引号。您可以尝试该答案中的解决方法,例如

#!/usr/bin/ksh

echo Start Executing SQL commands

# put the single-quotes in a variable to prevent the here-document from converting them to double
STR="'%XXBP%'"

array=$(sqlplus -s apps/apps << eof
SET PAGESIZE 0;
SELECT directory_name from all_directories where directory_name like $STR;
eof)

printf '%s\n' "${array[@]}"

关于sql - 将 SQL 结果传递到 shell 脚本中的数组时标识符无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53191195/

相关文章:

mysql - 连接日期函数mysql

linux - 我可以从 crontab 刷新 chromium 网页吗?

database - 小文件海量存储策略

arrays - 从 PL/SQL 中的多列关联数组中删除重复项

Java 读取 Oracle EXP 命令输出

sql - 搜索条件中

MySQL - 具有特定字符串的行到列

php - 关系表数据库

.net - 如何在负载下多路复用我的 asp.net web 应用程序?

sql - 在触发器中使用 DBMS_PIPE.PACK_MESSAGE 和 DBMS_PIPE.SEND_MESSAGE