如何从一个bash脚本中执行SQL? [英] How do you execute SQL from within a bash script?

查看:456
本文介绍了如何从一个bash脚本中执行SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想要自动执行某些SQL脚本。在过去,我已经使用SQL * Plus和手动名为sqlplus的二进制文件,从bash脚本。

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脚本的内部连接到数据库,并调用脚本的方式......所以,我可以插入日期,使查询的运行相对于过去若干天。

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:

内容的文件与-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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆