title: Msg 5011, Level 14, State 5, Line 11 ALTER DATABASE statement failed
body: <p>I ran the below scripts</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I got the error below </p>
  
  <blockquote>
    <p>Msg 5011, Level 14, State 5, Line 11<br>
    User does not have permission to alter database '@name', the database does not exist, or the database is not in a state that allows access checks.</p>
    
    <p>Msg 5069, Level 16, State 1, Line 11<br>
    ALTER DATABASE statement failed.</p>
  </blockquote>
  
  <p>Please let me know where I am stuck on how to resolve this issue.</p>
  
  <p>Note:- I ran this script in sa login.</p>