linux下如何将sql语句写为脚本

coolrain2008 2007-11-30 10:59:14
对数据库的一些操作我想在linux下面写成一个脚本,然后执行脚本,但不知道如何写.哪位大虾晓得~

下面是对数据库ORACLE的操作
system用户登录执行
drop user ebnewmt cascade

-- Create the user
create user EBNEWMT
identified by "123"
default tablespace EBNEWMT
temporary tablespace TEMP
profile DEFAULT;
-- Grant/Revoke role privileges
grant connect to EBNEWMT;
grant resource to EBNEWMT;
-- Grant/Revoke system privileges
grant unlimited tablespace to EBNEWMT;
...全文
742 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zglcl008 2007-12-07
  • 打赏
  • 举报
回复
还有可以通过sqlload 来实现一些功能。
mysql也可以这样实现。
mysql DatabaseName < 111.sql
zglcl008 2007-12-07
  • 打赏
  • 举报
回复
您说到:“我现在想将其写为一个脚本,然后做自动定时执行,但现在还是交互式的执行,不知道什么办法可以解决.不用输入用户和密码”
这个可以用crontab来实现 具体的您可以查找相关资料
我每天的定时处理就是用的crontab。。。。。。。。

shell下嵌入ora SQL一般是这样格式

#!/bin/sh
sqlplus /nolog < <EOF
conn /as user; //取得身份等
***** 操作语句
****
EOF
嵌入MySQL一般是这样格式
#!/bin/sh

mysql -u user -ppassword > $TempFile << eof
use database;
*****
*****
eof
coolrain2008 2007-12-05
  • 打赏
  • 举报
回复
是完整的sid.已经搞定了~脚本是这样写的
#!/bin/sh
sqlplus /nolog <<EOF
conn /as sysdba;
drop user ebnewmt cascade;
create user EBNEWMT identified by "123" default tablespace EBNEWMT temporary tablespace TEMP profile DEFAULT;
grant connect to EBNEWMT;
grant resource to EBNEWMT;
grant unlimited tablespace to EBNEWMT;
exit;
EOF
iambic 2007-12-04
  • 打赏
  • 举报
回复
哦,不好意思,是完整的sid?
我在windows下这样是可以的。Linux下还没试过。
iambic 2007-12-04
  • 打赏
  • 举报
回复
@test
test是什么?
coolrain2008 2007-12-04
  • 打赏
  • 举报
回复
我现在将命令和sql都写进脚本了,如下:
[oracle@outer_proj bin]$ vi sql.sh
#!/bin/sh
sqlplus system/123456@test <<EOF
drop user EPSTEST cascade;
create user EPSTEST identified by "123456" default tablespace EPSTEST temporary tablespace TEMP profile DEFAULT;
grant connect to EPSTEST;
grant resource to EPSTEST;
grant unlimited tablespace to EPSTEST;
>

但执行的时候有错误,哪位指导下啊~
[oracle@outer_proj bin]$ sql.sh

SQL*Plus: Release 10.2.0.1.0 - Production on Tue Dec 4 08:59:42 2007

Copyright (c) 1982, 2005, Oracle. All rights reserved.

ERROR:
ORA-12154: TNS:could not resolve the connect identifier specified


Enter user-name: SP2-0306: Invalid option.
Usage: CONN[ECT] [logon] [AS {SYSDBA|SYSOPER}]
where <logon> ::= <username>[/<password>][@<connect_identifier>] | /
Enter user-name: SP2-0306: Invalid option.
Usage: CONN[ECT] [logon] [AS {SYSDBA|SYSOPER}]
where <logon> ::= <username>[/<password>][@<connect_identifier>] | /
SP2-0157: unable to CONNECT to ORACLE after 3 attempts, exiting SQL*Plus
coolrain2008 2007-12-03
  • 打赏
  • 举报
回复
我明白你的意思,你说把sql语句写为一个脚本,然后用下面的命令执行
[oracle@outer_proj bin]$ sqlplus system/123456@test @sql.sh
SQL*Plus: Release 10.2.0.1.0 - Production on Mon Dec 3 10:24:20 2007

Copyright (c) 1982, 2005, Oracle. All rights reserved.

ERROR:
ORA-12154: TNS:could not resolve the connect identifier specified


Enter user-name: system
Enter password:


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options


User dropped.


User created.


Grant succeeded.


Grant succeeded.


Grant succeeded.


这样是能执行.但是我现在想要把sqlplus system/123456@test也写在脚本里.因为我需要定时自动执行这些,没有人工输入命令,到时间他自动执行一个脚本.但现在我得输入sqlplus system/123456@test @sql.sh才能执行,而且执行的时候还是交互式的,还的要输一次数据库的用户和密码.怎么样可以把sqlplus system/123456@test写在脚本里~
coolrain2008 2007-12-03
  • 打赏
  • 举报
回复
我的test包括IP了.它是个SID
iambic 2007-11-30
  • 打赏
  • 举报
回复
用sqlplus命令就可以吧:
[code=BatchFile]sqlplus username/password@db_host @scriptname[/code]
iambic 2007-11-30
  • 打赏
  • 举报
回复
你的格式怎么好像不全啊?数据库名、主机名、脚本名都不能少啊。
[code=BatchFile]sqlplus username/password@db_host @scriptname[/code]
iambic 2007-11-30
  • 打赏
  • 举报
回复
你的test是什么?包括ip了没有?
liusujian02 2007-11-30
  • 打赏
  • 举报
回复
在Unix下安装informix数据库的操作方法:

echo sql语句>sql.temp
dbaccess < sql.tmp
rm sql.temp

在linux下安装oracle,应该可以采用同样的方法~
coolrain2008 2007-11-30
  • 打赏
  • 举报
回复
你的办法是对,但是当我输入以后他还要提示输入用户名和密码,才能继续往下执行.如下:
[oracle@outer_proj ~]$ sqlplus system/123456@test

SQL*Plus: Release 10.2.0.1.0 - Production on Fri Nov 30 15:13:03 2007

Copyright (c) 1982, 2005, Oracle. All rights reserved.

ERROR:
ORA-12154: TNS:could not resolve the connect identifier specified


Enter user-name: system
Enter password:

Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL>

我现在想将其写为一个脚本,然后做自动定时执行,但现在还是交互式的执行,不知道什么办法可以解决.不用输入用户和密码

19,612

社区成员

发帖
与我相关
我的任务
社区描述
系统使用、管理、维护问题。可以是Ubuntu, Fedora, Unix等等
社区管理员
  • 系统维护与使用区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧