我在 SQL Server 2008 R2 中有一个数据库,我创建了这个用于恢复数据库的存储过程:
CREATE PROCEDURE [dbo].[usp_DBRestore]
@DBName nvarchar(60)
,@BackName nvarchar(120)
,@OutMessage nvarchar(4000) output
--,
--@DataName varchar(60),
--@DataFileName varchar(120),
--@LogName varchar(60),
--@LogFileName varchar(120)
AS
BEGIN TRY
USE [master]
ALTER DATABASE @DBName SET SINGLE_USER WITH ROLLBACK IMMEDIATE
RESTORE DATABASE @DBName FROM
DISK = @BackName WITH
FILE = 1, NOUNLOAD,
REPLACE,
PASSWORD = 'TEST'
SET @OutMessage = 'OK';
ALTER DATABASE @DBName SET MULTI_USER WITH ROLLBACK IMMEDIATE
END TRY
BEGIN CATCH
ALTER DATABASE @DBName SET MULTI_USER WITH ROLLBACK IMMEDIATE
INSERT [dbo].[ErrorLog]
(
[UserName],
[ErrorNumber],
[ErrorSeverity],
[ErrorState],
[ErrorProcedure],
[ErrorLine],
[ErrorMessage]
)
VALUES(
CONVERT(sysname, CURRENT_USER),
ERROR_NUMBER(),
ERROR_SEVERITY(),
ERROR_STATE(),
ERROR_PROCEDURE(),
ERROR_LINE(),
ERROR_MESSAGE()
)
END CATCH
当我执行代码时,我看到了这个错误:
过程、函数或触发器中不允许使用 USE 数据库语句。
我该如何解决这个错误?