如何在 SQL 中使用存储过程备份数据库?
How do I backup a database with a stored procedure in SQL?
我正在尝试创建一个备份数据库的存储过程,但是每当我执行它时,我要备份到的文件夹仍然是空的。
这是我的存储过程:
USE [HarvestMan_SoutheskFarm_03_05_22]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Will Sewell
-- Create date: 03-05-2022
-- Description: Procedure to back up a database
-- =============================================
ALTER PROCEDURE [dbo].[BackupDatabases]
@name VARCHAR(MAX) = 'HarvestMan_SoutheskFarm_03_05_22' -- DB NAME TO CREATE BACKUP
AS
BEGIN
DECLARE @path VARCHAR(256) -- path of backup files
DECLARE @fileName VARCHAR(256) -- filename for backup
DECLARE @fileDate VARCHAR(20) -- used for file name
SET @path = 'C:\Users\will.sewell\Documents\Database_Test'
-- specify filename format
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)
BEGIN
SET @fileName = @path + @name + '_' + @fileDate + '.BAK'
BACKUP DATABASE @name TO DISK = @fileName
END
END
我试过更改正在写入的驱动器。
我已经复制并粘贴了我要备份的数据库名称,以确保没有拼写错误。
我假设这可能是权限问题或文件夹中的可见文件。
当我执行存储过程时,我得到:
Processed 24472 pages for database 'HarvestMan_SoutheskFarm_03_05_22', file 'HarvestMan_dat' on file 2.
Processed 1 pages for database 'HarvestMan_SoutheskFarm_03_05_22', file 'HarvestMan_log' on file 2. BACKUP DATABASE successfully processed 24473 pages in 5.039 seconds (37.941 MB/sec).
Completion time: 2022-05-04T10:27:30.2344290+01:00
create proc SqlDBBackup
as
begin
declare @path varchar(1000);
set @path='D:\Backup\SampleDBTest3' + CONVERT(CHAR(10),
GETDATE(),
121) + '.bak';
BACKUP DATABASE SampleDBTest to DISK=@path;
end
我正在尝试创建一个备份数据库的存储过程,但是每当我执行它时,我要备份到的文件夹仍然是空的。
这是我的存储过程:
USE [HarvestMan_SoutheskFarm_03_05_22]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Will Sewell
-- Create date: 03-05-2022
-- Description: Procedure to back up a database
-- =============================================
ALTER PROCEDURE [dbo].[BackupDatabases]
@name VARCHAR(MAX) = 'HarvestMan_SoutheskFarm_03_05_22' -- DB NAME TO CREATE BACKUP
AS
BEGIN
DECLARE @path VARCHAR(256) -- path of backup files
DECLARE @fileName VARCHAR(256) -- filename for backup
DECLARE @fileDate VARCHAR(20) -- used for file name
SET @path = 'C:\Users\will.sewell\Documents\Database_Test'
-- specify filename format
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)
BEGIN
SET @fileName = @path + @name + '_' + @fileDate + '.BAK'
BACKUP DATABASE @name TO DISK = @fileName
END
END
我试过更改正在写入的驱动器。
我已经复制并粘贴了我要备份的数据库名称,以确保没有拼写错误。
我假设这可能是权限问题或文件夹中的可见文件。
当我执行存储过程时,我得到:
Processed 24472 pages for database 'HarvestMan_SoutheskFarm_03_05_22', file 'HarvestMan_dat' on file 2.
Processed 1 pages for database 'HarvestMan_SoutheskFarm_03_05_22', file 'HarvestMan_log' on file 2. BACKUP DATABASE successfully processed 24473 pages in 5.039 seconds (37.941 MB/sec).
Completion time: 2022-05-04T10:27:30.2344290+01:00
create proc SqlDBBackup
as
begin
declare @path varchar(1000);
set @path='D:\Backup\SampleDBTest3' + CONVERT(CHAR(10),
GETDATE(),
121) + '.bak';
BACKUP DATABASE SampleDBTest to DISK=@path;
end