Ⅰ 如何用cmd执行SQL语句
C:\Documents and Settings\Administrator>osql help
用法: osql [-U login id] [-P password]
[-S server] [-H hostname] [-E trusted connection]
[-d use database name] [-l login timeout] [-t query timeout]
[-h headers] [-s colseparator] [-w columnwidth]
[-a packetsize] [-e echo input] [-I Enable Quoted Identifiers]
[-L list servers] [-c cmdend] [-D ODBC DSN name]
[-q "cmdline query"] [-Q "cmdline query" and exit]
[-n remove numbering] [-m errorlevel]
[-r msgs to stderr] [-V severitylevel]
[-i inputfile] [-o outputfile]
[-p print statistics] [-b On error batch abort]
[-X[1] disable commands [and exit with warning]]
[-O use Old ISQL behavior disables the following]
<EOF> 批处理
控制台宽度自动调整
宽消息
默认错误级别为 — 1 对 1 这是我的CMD打印出来的,装了SQL SERVER 就行了执行语句需要在后面加GO1> select name from sysdatabases
2> go
Ⅱ 用cmd命令执行SQL脚本的问题
-- To allow advanced options to be changed.EXEC sp_configure 'show advanced options', 1GO-- To update the currently configured value for advanced options.RECONFIGUREGO-- To enable the feature.EXEC sp_configure 'xp_cmdshell', 1GO-- To update the currently configured value for this feature.RECONFIGUREGO这个先写了保证不会再Cmd中出错然后//这是判断是否有同名的数据库存在if exists (select * from sysdatabases where name = 'hotel')
drop database hotel
go//这是判断是否有同名的表if exists (select * from sysobjects where name = 'hotel')
drop table hotel
go
Ⅲ 如何使用sqlcmd在批处理脚本中执行SQL
使用sqlcmd可以在批处理脚本中执行SQL。虽然这个命令的参数很多,但幸运的是,我们不需要全部理解,在这里简要介绍以下几个:x0dx0a{ -U login_id [ -P password ] } | _E trusted connection }] 如果指定了-E就不需要指定用户名密码,当然指定了用户名密码就不用-E了;x0dx0a-S server_name [ \ instance_name ] 数据库服务器,如果不在本机的话必须指定;x0dx0a-d db_name 数据库名字,必须的;x0dx0a[ -i input_file ] [ -o output_file ] sql存在文件里的话用-i,输出到文件用-o;x0dx0a[ -q "cmdline query" ] [ -Q "cmdline query" and exit ] 输入是简单的sql,不用文件,推荐用-Q,如果你执行完sqlcmd还需要执行别的动作的话;x0dx0a[ -W remove trailing spaces ] 删除多余的空格,结果会更紧凑。x0dx0a当我们在真正的脚本中执行sql时,通常需要传入和输出变量。x0dx0a传入变量比较简单,如下所示:x0dx0asqlcmd -d test -Q "select * from dbo.Investment where investor=$(x)" -v x='IBM' -Wx0dx0a这个语句从test数据库的Investment表中选出investor等于x的所有行,注意到变量x被包含在${}中。x0dx0a然后用-v定义了x的值,'IBM'。x0dx0a-W确定输出的结果不包含多余的空格。x0dx0ax的值如果没有在sqlcmd中设定,系统会试图从别的地方去寻找,可能的地方包括,系统环境变量,用户环境变量,以及用在sqlcmd之前用set设定的变量值。x0dx0a如果你的数据里确实包含${},那么你并不希望进行变量的替换,使用-x选项可以禁止变量的替换。x0dx0a有些时候,你还希望能获得sql执行结果并保存到变量中。比如你们的日志系统每天都在产生日志文件,你要执行一个脚本来处理这些日志文件并存到数据库中。在处理之前,你必须读取数据库以确定上次处理到那一天了。你期望这样能解决你的问题:x0dx0asqlcmd -d test -Q "select ${x}=max(date) from dbo.logDates " -Wx0dx0a但这样并不工作。因为sqlcmd并不提供输出变量。x0dx0a不过你可以这样做:x0dx0asqlcmd -d test -Q "declare @x nvarchar(8);select @x=max(date) from dbo.logDates;print @x; " -Wx0dx0a这样你就可以得到一个干干净净的数字,而不会包含列名和其他信息。x0dx0a接着你将上述结果导入到一个文件里:x0dx0asqlcmd -d test -Q "declare @x nvarchar(8);select @x=max(date) from dbo.logDates;print @x; " -W 1.txtx0dx0a现在到了最关键的一步,将文件的内容写入到变量里:x0dx0aset /P myvar=<1.txtx0dx0a/p表明这个变量myvar的值需要用户输入;x0dx0a<1.txt表明从1.txt中读入而不是从其他地方读入。x0dx0a这样,我们就巧妙的把sql执行的结果写入到变量里了。
Ⅳ cmd直接执行sql脚本
以 mysql -h localhost -uroot -ppassword 方式直接将密码写入快捷方式,
登陆可以成功,但是如果使用 -e 参数执行语句,会出现
Warning: Using a password on the command line interface can be insecure 错误 ,
在MySQL 5.6.6之后可以使用 mysql_config_editor ,它可以把账户密码写入 */.mylogin.cnf 并加密
登入 MySQL Serverin 文件夹,执行
mysql_config_editor set --login-path=xxx --host=127.0.0.1 --user=root --password
--login-path 的值只是一个名字,可以随便写,
回车,然后输入密码,就完成了账户信息的写入
mysql_config_editor print --all
mysql --login-path=xxx
试一下,应该可以登入了
创建快捷方式,快捷方式的
目标:
"C:Program FilesMySQLMySQL Server 5.7inmysql.exe" "--defaults-file=C:ProgramDataMySQLMySQL Server 5.7my.ini" --login-path=try -e "SOURCE C:/Users/Desktop/main.sql;"
注意 -e 后面 路径中的反斜杠,写错了是无法运行的
起始位置:
"C:Program FilesMySQLMySQL Server 5.7in"
Ⅳ cmd 执行sql脚本,该怎么处理
在dos下运行 :
cmd /c osql -S"127.0.0.1" -U"sa" -P"123" -d"Test" -i"E:\资料\学习\MSSQL\Dos执行文件\test.sql"
-S :数据库IP地址
-U:用户名
-P:密码
-d:数据库名
-i :数据库文件
注意:在没有装有数据库的情况下 , 你的系统中可能没有osql工具的环境 , 此时我们只需要将 OSQL.EXE和osql.rll两个文件拷到c:\window\system32目录下即可
Ⅵ 如何用cmd执行SQL语句
isql -Usa -P123456 -Sservername(服务器名称) -o e:\test (如果要将执行结果输出则可加-o参数,如不需要则不用,当然还有很多其它参数可以使用,但不是必须的,具体可以搜一下)
user dbname(实际数据库名称)
go
其它要执行的SQL语句
go
Ⅶ cmd怎么执行sql脚本
先使用cmd连接并打开数据库,然后才能执行SQL脚本。
Ⅷ 怎样执行.sql文件
执行.sql文件,可以在mysql命令行里面执行,步骤如下:
1.使用cmd命令执行(windows下,unix或linux在的其控制台下)
2.【Mysql的bin目录】mysql –u用户名 –p密码 –D数据库<【sql脚本文件路径全名】,示例:
D:mysqlinmysql –uroot –p123456 -Dtest
注意:
A、如果在sql脚本文件中使用了use 数据库,则-D数据库选项可以忽略
B、如果【Mysql的bin目录】中包含空格,则需要使用“”包含,如:“C:Program Filesmysqlinmysql” –u用户名 –p密码 –D数据库<【sql脚本文件路径全名】