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
本文转自守住每一天51CTO博客,原文链接:http://blog.51cto.com/liuyu/278036,如需转载请自行联系原作者