想要创建一个批处理文件来执行SQL脚本 [英] Want to create a batch file to execute SQL script

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

问题描述

嗨..



如您所知,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 the ShrinkDatabase 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. Create C:\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.log

After execution, C:\BAT\ShrinkDB.log will contain the output of SQLCMD.

2. Create C:\BAT\SHRINKDB.SQL containing the SQL commands:

ALTER database dbName SET RECOVERY simple
GO
DBCC shrinkdatabase (dbName)
GO



3. Use Windows Task Scheduler to schedule C:\BAT\ShrinkDB.bat


这篇关于想要创建一个批处理文件来执行SQL脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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