从命令行执行 SQL 脚本 [英] Execute SQL script from command line

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

问题描述

我需要使用批处理文件更改数据库,举个简单的例子,删除一个表.我正在使用带有用户 sa 及其密码的本地 SQL Express (SQL Server 2008 R2).

I need to alter a database using a batch file, for a simple example, drop a table. I´m using local SQL Express (SQL Server 2008 R2) with user sa and its password.

bat 文件会怎样?

如何在脚本中指定密码以及我在 SQL Express 中使用的密码?

How can I specify in the script the password and that I use in SQL Express?

推荐答案

查看 sqlcmd 实用程序.它允许您从命令行执行 SQL.

Take a look at the sqlcmd utility. It allows you to execute SQL from the command line.

http://msdn.microsoft.com/en-us/library/ms162773.aspx

文档里都有,但语法应该是这样的:

It's all in there in the documentation, but the syntax should look something like this:

sqlcmd -U myLogin -P myPassword -S MyServerName -d MyDatabaseName 
    -Q "DROP TABLE MyTable"

这篇关于从命令行执行 SQL 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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