0

我使用此查询在bat文件到备份数据库,如何通过bat文件备份sql server 2014 db数据?

sqlcmd -S .\MSSQLSERVER -E -Q "EXEC sp_BackupDb @backupLocation='D:\SQLBackups\', @databaseName='TestDb', @backupType='F'" pause 

它抛出这个错误,

Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : SQL Server Network Inte rfaces: Connection string is not valid [87]. Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : Login timeout expired. Sqlcmd: Error: Microsoft ODBC Driver 11 for SQL Server : A network-related or in stance-specific error has occurred while establishing a connection to SQL Server . Server is not found or not accessible. Check if instance name is correct and i f SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..

是否有人可以帮忙吗?提前致谢。

+0

尝试从服务器名称规范中删除实例名称:'-S .'而不是'-S。\ MSSQLSERVER'。 –

+0

sqlcmd -S。\ MSSQLSERVER -E -Q“select 1”是否返回错误?l – PollusB

回答

0

可以使用SQLCMD在.bat文件,使这个bakcup,在这个环节上,你有一个例子stackoverflow link

SQLCMD

The sqlcmd utility lets you enter Transact-SQL statements, system procedures, and script files at the command prompt, in Query Editor in SQLCMD mode, in a Windows script file or in an operating system (Cmd.exe) job step of a SQL Server Agent job. This utility uses OLE DB to execute Transact-SQL batches.

这里是链接的例子和定义: msdn link definition

例如:

sqlcmd -E -S %SERVERNAME% -d master -Q "BACKUP DATABASE [%DATABASENAME%] TO DISK = N'%BACKUPFILENAME%' WITH INIT , NOUNLOAD , NAME = N'%DATABASENAME% backup', NOSKIP , STATS = 10, NOFORMAT" 

希望对您有所帮助!

相关问题