语法查错
CREATE PROCEDURE sp_backup
@dbname varchar(20),--数据库名称
@db_bak varchar(20),--备份设备
@log_bak varchar(20)--日志备份设备
AS
ALTER DATABASE @dbname SET RECOVERY FULL
BACKUP DATABASE @dbname TO @db_bak
BACKUP LOG @dbname TO @log_bak
GO
------解决方案-------------------- 如果数据库名是动态的,需要用动态SQL语句。
------解决方案----------------------改成这样
CREATE PROCEDURE sp_backup
@dbname varchar(20),--数据库名称
@db_bak varchar(20),--备份设备
@log_bak varchar(20)--日志备份设备
AS
exec sp_executesql
N 'ALTER DATABASE @dbname SET RECOVERY FULL
BACKUP DATABASE @dbname TO @db_bak
BACKUP LOG @dbname TO @log_bak '
,N '@dbname varchar(20),@db_bak varchar(20),@log_bak varchar(20) '
,@dbname,@db_bak,@log_bak
GO
------解决方案--------------------类似:
declare @sql varchar(100)
set @sql = 'ALTER DATABASE ' + @dbname + ' SET RECOVERY FULL '
exec (@sql)