How do you execute SQL from within a bash script?(如何从 bash 脚本中执行 SQL?)
问题描述
我有一些试图自动化的 SQL 脚本.过去,我使用过 SQL*Plus,并从 bash 脚本手动调用了 sqlplus 二进制文件.
I have some SQL scripts that I'm trying to automate. In the past I have used SQL*Plus, and called the sqlplus binary manually, from a bash script.
但是,我试图弄清楚是否有办法连接到数据库,并从 bash 脚本内部调用脚本......这样我就可以插入 date 并制作查询相对于过去的特定天数运行.
However, I'm trying to figure out if there's a way to connect to the DB, and call the script from inside of the bash script... so that I can insert date and make the queries run relative to a certain number of days in the past.
推荐答案
我有点困惑.您应该能够从 bash 脚本中调用 sqlplus.这可能就是你在第一条语句中所做的
I'm slightly confused. You should be able to call sqlplus from within the bash script. This may be what you were doing with your first statement
尝试在您的 bash 脚本中执行以下操作:
Try Executing the following within your bash script:
#!/bin/bash
echo Start Executing SQL commands
sqlplus <user>/<password> @file-with-sql-1.sql
sqlplus <user>/<password> @file-with-sql-2.sql
如果您希望能够将数据传递到您的脚本中,您可以通过 SQLPlus 通过将参数传递到脚本中来实现:
If you want to be able to pass data into your scripts you can do it via SQLPlus by passing arguments into the script:
file-with-sql-1.sql
select * from users where username='&1';
然后改bash脚本调用sqlplus传入值
Then change the bash script to call sqlplus passing in the value
#!/bin/bash
MY_USER=bob
sqlplus <user>/<password> @file-with-sql-1.sql $MY_USER
这篇关于如何从 bash 脚本中执行 SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 bash 脚本中执行 SQL?
基础教程推荐
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
