Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server kup命令,例如使用不正确的数据库名,将出现2个错误。 备份数据库错误\u数据库\u名称到磁盘='drive:\path\filename.bak' Msg 911, Level 16, State 11, Line 1 Could not locate entry in sysdatabases for database 'incorrect_database_name'. No entry found with that name. Make sure that the name is entered correctly. Msg 3013, Level 16, State 1, Line 1 BACKUP DATABASE is terminating abnormally._Sql Server_Database Restore - Fatal编程技术网

Sql server kup命令,例如使用不正确的数据库名,将出现2个错误。 备份数据库错误\u数据库\u名称到磁盘='drive:\path\filename.bak' Msg 911, Level 16, State 11, Line 1 Could not locate entry in sysdatabases for database 'incorrect_database_name'. No entry found with that name. Make sure that the name is entered correctly. Msg 3013, Level 16, State 1, Line 1 BACKUP DATABASE is terminating abnormally.

Sql server kup命令,例如使用不正确的数据库名,将出现2个错误。 备份数据库错误\u数据库\u名称到磁盘='drive:\path\filename.bak' Msg 911, Level 16, State 11, Line 1 Could not locate entry in sysdatabases for database 'incorrect_database_name'. No entry found with that name. Make sure that the name is entered correctly. Msg 3013, Level 16, State 1, Line 1 BACKUP DATABASE is terminating abnormally.,sql-server,database-restore,Sql Server,Database Restore,如果您想知道为什么备份在try-a-catch中失败的实际错误,存储过程会屏蔽它 现在,谈谈你的问题。。我要做的是,当还原成功时,我会立即删除.bak或将其移动到新位置,从而将其从参数中指定的目录中删除。失败时,catch语句可以包含一个GOTO,它会将您带回BEGIN TRY之前并开始执行它停止的位置,因为它不会递归地检测您从目录中移动的文件 RUN_AGAIN: BEGIN TRY RECURSIVE DIR FOR FILENAMES RESTORE DATABASE... ON SUCC

如果您想知道为什么备份在try-a-catch中失败的实际错误,存储过程会屏蔽它

现在,谈谈你的问题。。我要做的是,当还原成功时,我会立即删除.bak或将其移动到新位置,从而将其从参数中指定的目录中删除。失败时,catch语句可以包含一个GOTO,它会将您带回BEGIN TRY之前并开始执行它停止的位置,因为它不会递归地检测您从目录中移动的文件

RUN_AGAIN:
BEGIN TRY
RECURSIVE DIR FOR FILENAMES
RESTORE DATABASE...
ON SUCCEED, DELETE .BAK FILE
END TRY
BEGIN CATCH
ON FAILURE, MOVE .BAK to A SAFE LOCATION FOR LATER ANALYSIS
GOTO RUN_AGAIN
END CATCH
我不是说它漂亮,但它会起作用的。您不能将GOTO引用放在TRY/CATCH块内,因此它必须在它之外


不管怎样,我只是想,即使这个问题已经很老了,我还是会添加我的想法,只是为了帮助处于相同情况的其他人。

此外,如果您没有从文件列表中删除空记录(因为它已被注释掉),那么当您的循环从0开始时,它会在最后一次迭代中处理一个不存在的文件

而不是
@index=0
,它应该是
@index=1

或取消注释
delete from@backup\u files,其中file\u path为空

如果您正在执行与数据库恢复一样重要的操作,我个人不会让脚本从目录中获取内容。特别是在一个关键的系统上。我的观点是正确的。但是,这只是为了自动设置我们的开发环境。感谢您指出您看到的问题,但我不确定您对脚本所做的更改是如何回答原始问题的。您似乎为Try-and-Catch添加了打印语句。我对进入try-catch的控制没有问题。我的问题是,如果我没有将逻辑放入try-catch中,脚本就可以工作,但是如果我将其包装到try-catch中,脚本就会失败。
ALTER proc usp_restore_databases
(
    @source_directory varchar(1000),
    @restore_directory varchar(1000)
)
as
begin 
    declare @number_of_backup_files int

  begin transaction
  begin try
    print 'Entering TRY...'
--     step 0: Initial validation

        if(right(@source_directory, 1) <> '\') set @source_directory = @source_directory + '\'
        if(right(@restore_directory, 1) <> '\') set @restore_directory = @restore_directory + '\'

  --   step 1: Put all the backup files in the specified directory in a table -- 

        declare @backup_files table ( file_path varchar(1000))

        declare @dos_command varchar(1000)
        set @dos_command = 'dir ' + '"' + @source_directory + '*.bak" /s/b'

        /* DEBUG */ print @dos_command

        insert into @backup_files(file_path) exec xp_cmdshell  @dos_command

        --delete from @backup_files where file_path IS NULL

        select @number_of_backup_files = count(1) from @backup_files

        /* DEBUG */ select * from @backup_files
        /* DEBUG */ print @number_of_backup_files

    -- step 2: restore each backup file --

        declare backup_file_cursor cursor for select file_path from @backup_files
        open  backup_file_cursor

        declare @index int; set @index = 0
        while(@index < @number_of_backup_files)
        begin


                declare @backup_file_path varchar(1000)
                fetch next from backup_file_cursor into @backup_file_path

                /* DEBUG */ print @backup_file_path

      --           step 2a: parse the full backup file name to get the DB file name.    
                declare @db_name varchar(100)

                set @db_name = right(@backup_file_path, charindex('\', reverse(@backup_file_path)) -1)  -- still has the .bak extension
                /* DEBUG */ print @db_name

                set @db_name = left(@db_name, charindex('.', @db_name) -1)                      
                /* DEBUG */ print @db_name

                set @db_name = lower(@db_name)
                /* DEBUG */ print @db_name

        --         step 2b: find out the logical names of the mdf and ldf files
                declare @mdf_logical_name varchar(100),
                                @ldf_logical_name varchar(100)

                declare @backup_file_contents table     
                (
                        LogicalName nvarchar(128),
                        PhysicalName nvarchar(260),
                        [Type] char(1),
                        FileGroupName nvarchar(128),
                        [Size] numeric(20,0),
                        [MaxSize] numeric(20,0),
                        FileID bigint,
                        CreateLSN numeric(25,0),
                        DropLSN numeric(25,0) NULL,
                        UniqueID uniqueidentifier,
                        ReadOnlyLSN numeric(25,0) NULL,
                        ReadWriteLSN numeric(25,0) NULL,
                        BackupSizeInBytes bigint,
                        SourceBlockSize int,
                        FileGroupID int,
                        LogGroupGUID uniqueidentifier NULL,
                        DifferentialBaseLSN numeric(25,0) NULL,
                        DifferentialBaseGUID uniqueidentifier,
                        IsReadOnly bit,
                        IsPresent bit 
                )

                insert into @backup_file_contents 
                exec ('restore filelistonly from disk=' + '''' + @backup_file_path + '''')

                select @mdf_logical_name = LogicalName from @backup_file_contents where [Type] = 'D'
                select @ldf_logical_name = LogicalName from @backup_file_contents where [Type] = 'L'

                /* DEBUG */ print @mdf_logical_name + ', ' + @ldf_logical_name

          --       step 2c: restore

                declare @mdf_file_name varchar(1000),
                                @ldf_file_name varchar(1000)

                set @mdf_file_name = @restore_directory + @db_name + '.mdf'
                set @ldf_file_name = @restore_directory + @db_name + '.ldf'

                /* DEBUG */ print   'mdf_logical_name = ' + @mdf_logical_name + '|' +
                                                        'ldf_logical_name = ' + @ldf_logical_name + '|' +
                                                        'db_name = ' + @db_name + '|' +
                                                        'backup_file_path = ' + @backup_file_path + '|' +
                                                        'restore_directory = ' + @restore_directory + '|' +
                                                        'mdf_file_name = ' + @mdf_file_name + '|' +
                                                        'ldf_file_name = ' + @ldf_file_name
print @index

              --  restore database @db_name from disk = @backup_file_path 
              --  with
              --          move @mdf_logical_name to @mdf_file_name,
              --          move @ldf_logical_name to @ldf_file_name

            --     step 2d: iterate
                set @index = @index + 1

        end

        close backup_file_cursor
        deallocate backup_file_cursor

  end try
  begin catch
        print 'Entering Catch...'
        print error_message()
      rollback transaction
      return
  end catch

  commit transaction

end
Msg 911, Level 16, State 11, Line 1
Could not locate entry in sysdatabases for database 'incorrect_database_name'. No entry found with that name. Make sure that the name is entered correctly.
Msg 3013, Level 16, State 1, Line 1
BACKUP DATABASE is terminating abnormally.
RUN_AGAIN:
BEGIN TRY
RECURSIVE DIR FOR FILENAMES
RESTORE DATABASE...
ON SUCCEED, DELETE .BAK FILE
END TRY
BEGIN CATCH
ON FAILURE, MOVE .BAK to A SAFE LOCATION FOR LATER ANALYSIS
GOTO RUN_AGAIN
END CATCH