转自:http://liuyu.blog.51cto.com/183345/278036
今天项目中需要写一个从oracle到oracle同步数据的工具,准备用shell写一下。主要是项目上线时跑跑。用shell写便于在更新脚本中调用。
How do I call Oracle or MySQL sql statements in UNIX / Linux shell scripts?
MySQL: Use SQL Directly In Shell Scripts
#!/bin/sh user="dbuser" pass="dbpassword" db="dbnme" mysql -u "$user" -p"$pass" "$db" <<EOF sql-statement-1; sql-statement-2; EOF
Using Shell Variables In SQL
#!/bin/sh user="dbuser" pass="dbpassword" db="dbnme" sql="select * from tal_name" mysql -u "$user" -p"$pass" <<EOF use $db; $sql; EOF
Oracle: Use SQL Directly In Shell Scripts
#!/bin/sh user="system" pass="manager" sqlplus -S $user/$pass <<EOF sql-statement-1; sql-statement-2; exit; EOF
Using Shell Variables In SQL
#!/bin/sh user="system" pass="manager" var="$1" sqlplus -S $user/$pass <<EOF SELECT * FROM tableName WHERE username=$var; exit; EOF
You can call sql statements from .sql file itself as follows:
#!/bin/ksh
sqlplus -S system/manager @my_sql_script.sql