SQL Server备份和收缩脚本问题

时间:2014-11-18 15:12:31

标签: sql-server sql-server-2008 sql-server-2012

我在使备份/收缩脚本正常工作时遇到问题。

我一直收到错误

  

Msg 102,Level 15,State 1,Line 12
  'CONCAT'附近的语法不正确。

     

Msg 102,Level 15,State 1,Line 15
  'CONCAT'附近的语法不正确。

每次尝试运行时都会出现

错误。

你能帮忙吗?

Declare @dbname VarChar(250);
Declare @logfile VarChar(1000);
Declare @baklocation VarChar(1000) = 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\BACKUP\';

Select @dbname = name From sys.databases Where name Not In ('master', 'tempdb', 'model', 'msdb');
While @@rowcount <> 0
Begin
    Alter DATABASE [@dbname] Set RECOVERY SIMPLE With NO_WAIT;

    Backup Database @dbname 
        TO DISK = CONCAT(@baklocation, @dbname, '\', @dbname, '_', Convert(Varchar(500),GetDate(),112), '.bak');

    Backup Log @dbname 
        TO DISK = CONCAT(@baklocation, @dbname, '\', @dbname, '_', Convert(Varchar(500),GetDate(),112), '.log.bak');

End;

SELECT @logfile = filename FROM SYSALTFILES Where groupid = 0 AND dbid > 4;
While @@rowcount <> 0
Begin
    DBCC SHRINKFILE(@logfile, 1);
End;

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

CONCAT于2012年或更高版本

提供

因此,创建变量并将这些值分配给IT

Source

Declare @dbname VarChar(250);
Declare @logfile VarChar(1000);
Declare @baklocation VarChar(1000) = 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\BACKUP\';
Declare @Newvar nvarchar(MAx)

Select @dbname = name From sys.databases Where name Not In ('master', 'tempdb', 'model', 'msdb');
While @@rowcount <> 0
Begin
    Alter DATABASE [@dbname] Set RECOVERY SIMPLE With NO_WAIT;
set @Newvar= @baklocation + @dbname + '\' + @dbname + '_' + Convert(Varchar(500),GetDate(),112)+ '.bak'
    Backup Database @dbname 
        TO DISK =@Newvar ;

    Backup Log @dbname 
        TO DISK = @Newvar;

End;

SELECT @logfile = filename FROM SYSALTFILES Where groupid = 0 AND dbid > 4;
While @@rowcount <> 0
Begin
    DBCC SHRINKFILE(@logfile, 1);
End;