2013-10-30 51 views
0

我跑到下面的腳本消息5011,級別14,狀態5,第11行ALTER DATABASE語句失敗

DECLARE @name VARCHAR(50) 

DECLARE @Statment VARCHAR(500) 

DECLARE DB_cursor CURSOR 
FOR 
SELECT name 
FROM sys.databases 
WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb', 
         'ReportServer$SQLEXPRESS', 
         'ReportServer$SQLEXPRESSTempDB') 

OPEN DB_cursor 

FETCH NEXT FROM DB_cursor INTO @name 

WHILE @@FETCH_STATUS = 0 
BEGIN 

    PRINT @name 

    ALTER DATABASE [@name] SET SINGLE_USER WITH ROLLBACK IMMEDIATE 

    SET @Statment = 'Backup database ' + @name + ' To disk =N' + '''' 
     + 'C:\Mas\' + @name + '.bak' + '''' 

    PRINT @Statment 

    EXEC(@Statment) 
-- ALTER DATABASE [@name] SET MULTI_USER WITH ROLLBACK IMMEDIATE 
-- EXEC sp_dboption @name, N'offline', N'false' 
    FETCH NEXT FROM DB_cursor INTO @name 
END 
CLOSE DB_cursor 
DEALLOCATE DB_cursor 

我下面

消息5011,級別14,狀態5錯誤,第11行
用戶沒有權限更改數據庫'@name',數據庫不存在或數據庫未處於允許訪問檢查的狀態。

Msg 5069,Level 16,State 1,Line 11
ALTER DATABASE語句失敗。

請讓我知道我被困在如何解決這個問題。

注意: - 我運行這個腳本在登錄。

+0

路徑,爲什麼你需要使數據庫單個用戶運行備份? ?? –

回答

2

錯誤消息明確指出原因。這是因爲Alter database命令將[@name]視爲數據庫而不是佔位符。您可以在動態查詢中包含這兩個語句,然後執行。下面應該工作:

DECLARE @name VARCHAR(50) 
DECLARE @Statment VARCHAR(500) 
DECLARE @Statment1 VARCHAR(500) 
DECLARE @Statment2 VARCHAR(500) 

select name from sys.databases where name NOT IN('master','tempdb','model','msdb','ReportServer$SQLEXPRESS','ReportServer$SQLEXPRESSTempDB') 

OPEN DB_cursor 

FETCH NEXT FROM DB_cursor INTO @name 

WHILE @@FETCH_STATUS = 0 

BEGIN 

Print @name 

SET @Statment = 'ALTER DATABASE ' + @name + ' SET SINGLE_USER WITH ROLLBACK IMMEDIATE; ' 
EXEC(@Statment) 
    SET @Statment1 = 'BACKUP DATABASE ' + @name + ' To disk =N' +''''+ 'C:\Mas\'+ @name   +'.bak'+'''' 
EXEC(@Statment1) 
SET @Statment2 = 'ALTER DATABASE ' + @name + ' SET MULTI_USER WITH ROLLBACK IMMEDIATE; ' 
EXEC(@Statment2) 

EXEC(@Statment) 
-- ALTER DATABASE [@name] SET MULTI_USER WITH ROLLBACK IMMEDIATE 
-- EXEC sp_dboption @name, N'offline', N'false' 
FETCH NEXT FROM DB_cursor INTO @name 
END 
CLOSE DB_cursor 
DEALLOCATE DB_cursor 
0

SQL命令ALTER DATABASE不允許參數,因此它將@name視爲實際名稱而不是參數。

ALTER DATABASE

NAME 指定文件的邏輯名稱。 logical_file_name 是引用文件時在Microsoft SQL Server中使用的名稱。該名稱在數據庫中必須是唯一的,並符合標識符的規則。名稱可以是字符或Unicode常量,常規標識符或分隔標識符。有關更多信息,請參閱Using Identifiers

0

您的數據庫

DECLARE @A NVARCHAR(800) = ' ALTER DATABASE [' + 
          @name + 
          '] SET SINGLE_USER WITH ROLLBACK IMMEDIATE ' 
EXECUTE Sp_ExecuteSQL @A 

的變化狀態最有用SP_ExecuteSQL然後你就可以在單用戶模式設置數據庫。

1

我改變你的T-SQL,這工作。

你最改變備份文件的目的地在這個樣本的花葯路徑。

DECLARE @name VARCHAR(50) 

DECLARE @Statment VARCHAR(500) 

DECLARE DB_cursor CURSOR 
FOR 
SELECT name 
FROM sys.databases 
WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb', 
         'ReportServer$SQLEXPRESS', 
         'ReportServer$SQLEXPRESSTempDB') 

OPEN DB_cursor 

FETCH NEXT FROM DB_cursor INTO @name 

WHILE @@FETCH_STATUS = 0 
    BEGIN 

    PRINT @name 
--  DECLARE @A NVARCHAR(800) = ' ALTER DATABASE [' + @name + '] SET Multi_USER WITH ROLLBACK IMMEDIATE ' 
    DECLARE @A NVARCHAR(800) = ' ALTER DATABASE [' + @name + '] SET SINGLE_USER WITH ROLLBACK IMMEDIATE ' 
    EXECUTE Sp_ExecuteSQL @A 

    SET @Statment = 'Backup database ' + @name + ' To disk =N' + '''' 
     + 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER2012\MSSQL\Backup\' 
     + @name + '.bak' + '''' 

    PRINT @Statment 

    EXEC(@Statment) 
-- ALTER DATABASE [@name] SET MULTI_USER WITH ROLLBACK IMMEDIATE 
-- EXEC sp_dboption @name, N'offline', N'false' 
    FETCH NEXT FROM DB_cursor INTO @name 
END 
CLOSE DB_cursor 
DEALLOCATE DB_cursor 

像縮小圖片

你最篦訪問SQL Server的用戶在你的Back up

enter image description here

相關問題