我在使备份/收缩脚本正常工作时遇到问题。
我一直收到错误
每次尝试运行时都会出现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;
答案 0 :(得分:1)
你可以试试这个:
CONCAT于2012年或更高版本
提供因此,创建变量并将这些值分配给IT
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;