想要创建一个批处理文件来执行SQL脚本 [英] Want to create a batch file to execute SQL script
问题描述
嗨..
如您所知,MS-SQL服务器生成事务日志文件。经过这么多交易后,它变得巨大。我希望缩小它们并在定期间隔后释放内存。
我知道如何使用sqlcmd。
Server_Name -U username -P passsword
GO
ALTER 数据库 dbName SET RECOVERY simple
GO
DBCC shrinkdatabase(dbName)
GO
现在我想自动执行这个脚本。我正在考虑使用任务调度程序,以便我可以在特定时间触发这些命令。
问候和谢谢。
我从来不需要运行ShrinkDatabase
命令但我有其他BAT文件用于我的数据库的计划维护。下面是使用SQL命令的示例。
1.创建C:\ BAT \ SHRINKDB.BAT
包含以下声明:
cd C:\Program Files \ Microsoft Microsoft SQL Server \110\Tools\Binn
SQLCMD -S YOURCOMPUTERNAME \ SQLEXPRESS -E -w 166 -e -i C:\ BAT \ShrinkDB.sql -o C:\ BAT \ShrinkDB.log执行后,C:\ BAT \ShrinkDB .log将包含SQLCMD的输出。
2.创建C:\ BAT \ SHRINKDB.SQL
包含SQL命令:
ALTER数据库dbName SET RECOVERY simple
GO
DBCC shrinkdatabase(dbName)
GO
3.使用Windows任务计划程序安排C:\ BAT \ShrinkDB.bat
Hi..
As you know, MS-SQL server generates transaction log file. After so much transaction it becomes huge. I want to shrink those and release the memory after regular interval of time.
I know how to do it using sqlcmd.
Server_Name -U username -P passsword
GO
ALTER database dbName SET RECOVERY simple
GO
DBCC shrinkdatabase (dbName)
GO
Now I want to execute this script automatically. I am thinking to use task scheduler so that I can trigger these commands on specific time.
Regards and Thanks.
I have never needed to run theShrinkDatabase
command but I have other BAT files that I use for scheduled maintenance of my database. Below is an example using your SQL commands.
1. CreateC:\BAT\SHRINKDB.BAT
containing these statements:
cd C:\Program Files\Microsoft SQL Server\110\Tools\Binn SQLCMD -S YOURCOMPUTERNAME\SQLEXPRESS -E -w 166 -e -i C:\BAT\ShrinkDB.sql -o C:\BAT\ShrinkDB.logAfter execution, C:\BAT\ShrinkDB.log will contain the output of SQLCMD.
2. CreateC:\BAT\SHRINKDB.SQL
containing the SQL commands:
ALTER database dbName SET RECOVERY simple GO DBCC shrinkdatabase (dbName) GO
3. Use Windows Task Scheduler to scheduleC:\BAT\ShrinkDB.bat
这篇关于想要创建一个批处理文件来执行SQL脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!