组合两个 .bat 文件 Dos 和 T-SQL

发布于 2024-10-06 09:50:52 字数 1987 浏览 5 评论 0原文

我想要的是将这两个文件合并为一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱ヾ落雨殇 2024-10-13 09:50:52

添加

quit

为 .SQL 文件的最后一行。

Add

quit

as the last line of the .SQL files.

能否归途做我良人 2024-10-13 09:50:52

我发现使用两个文件有效

I found using two files works

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文