组合两个 .bat 文件 Dos 和 T-SQL
我想要的是将这两个文件合并为一个 bat 文件,但目前 sqlcmd 停止并显示 sqlqcmd 提示符,并且 SQL 未运行。
哎哟服务器坏了,所以我需要采取应急措施 PDQ
我通常使用 SQL Server 代理来完成一些工作,但在对新服务器进行排序时。我只有 SQL Express 没有 SQL Server 代理。
很好,所以我创建了一个 backup.bat,它在 cmd 中运行良好,调用 backup.sql,sql 将其发送到另一个驱动器。
我使用的语句是:-
Sqlcmd –S \mypc\instancename –i C:\backup.sql(因为我是本地窗口身份验证就可以了)
这样我就可以在不太友好的 Windows 7 中使用任务计划
程序我有一个调用 .sql 文件的 .bat 文件。
Backup.bat
Sqlcmd –S \mypc\instancename –i C:\backup.sql
Backup.sql
BACKUP DATABASE [Northwind] TO DISK = N'\\nas1\backup\northwind.bak' WITH NOFORMAT, INIT, NAME = N'Northwind-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
declare @backupSetId as int
select @backupSetId = position from msdb..backupset where database_name=N'Northwind' and backup_set_id=(select max(backup_set_id) from msdb..backupset where database_name=N'Northwind' )
if @backupSetId is null begin raiserror(N'Verify failed. Backup information for database ''Northwind'' not found.', 16, 1) end
RESTORE VERIFYONLY FROM DISK = N'\\nas1\backup\Northwind.bak' WITH FILE = @backupSetId, NOUNLOAD, NOREWIND
GO
我想要的是将这两个文件合并为一个 bat 文件,但目前 sqlcmd 停止并显示 sqlcmd 提示符,并且 SQL 未运行。
Sqlcmd –S \mypc\instancename
BACKUP DATABASE [Northwind] TO DISK = N'\\nas1\backup\northwind.bak' WITH NOFORMAT, INIT, NAME = N'Northwind-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
declare @backupSetId as int
select @backupSetId = position from msdb..backupset where database_name=N'Northwind' and backup_set_id=(select max(backup_set_id) from msdb..backupset where database_name=N'Northwind' )
if @backupSetId is null begin raiserror(N'Verify failed. Backup information for database ''Northwind'' not found.', 16, 1) end
RESTORE VERIFYONLY FROM DISK = N'\\nas1\backup\Northwind.bak' WITH FILE = @backupSetId, NOUNLOAD, NOREWIND
GO
What I would like is to combine the two files into one bat file but currently sqlcmd stops with the sqlqcmd prompt and the SQL is not running.
Ouch server went bang so I need to put contingency in place PDQ
I normally use SQL server agent with some jobs but while the new server is being sorted. I only have SQL express No SQL server agent.
Fine my so I have created a backup.bat it works fine in cmd calling backup.sql the sql send it to another drive.
The statement I used is:-
Sqlcmd –S \mypc\instancename –i C:\backup.sql( as I am local window authentication is fine)
This is so that i can use task scheduler in not very friendly windows 7
At the moment I have a .bat file calling a .sql file.
Backup.bat
Sqlcmd –S \mypc\instancename –i C:\backup.sql
Backup.sql
BACKUP DATABASE [Northwind] TO DISK = N'\\nas1\backup\northwind.bak' WITH NOFORMAT, INIT, NAME = N'Northwind-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
declare @backupSetId as int
select @backupSetId = position from msdb..backupset where database_name=N'Northwind' and backup_set_id=(select max(backup_set_id) from msdb..backupset where database_name=N'Northwind' )
if @backupSetId is null begin raiserror(N'Verify failed. Backup information for database ''Northwind'' not found.', 16, 1) end
RESTORE VERIFYONLY FROM DISK = N'\\nas1\backup\Northwind.bak' WITH FILE = @backupSetId, NOUNLOAD, NOREWIND
GO
What I want is to combine the two files into one bat file but currently sqlcmd stops with the sqlcmd prompt and the SQL is not running.
Sqlcmd –S \mypc\instancename
BACKUP DATABASE [Northwind] TO DISK = N'\\nas1\backup\northwind.bak' WITH NOFORMAT, INIT, NAME = N'Northwind-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
declare @backupSetId as int
select @backupSetId = position from msdb..backupset where database_name=N'Northwind' and backup_set_id=(select max(backup_set_id) from msdb..backupset where database_name=N'Northwind' )
if @backupSetId is null begin raiserror(N'Verify failed. Backup information for database ''Northwind'' not found.', 16, 1) end
RESTORE VERIFYONLY FROM DISK = N'\\nas1\backup\Northwind.bak' WITH FILE = @backupSetId, NOUNLOAD, NOREWIND
GO
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加
为 .SQL 文件的最后一行。
Add
as the last line of the .SQL files.
我发现使用两个文件有效
I found using two files works