自动获取要执行sql的当前数据库进行备份数据库
DECLARE @sqlstr NVARCHAR(MAX)=''
DECLARE @filename NVARCHAR(MAX)=''
DECLARE @dbname NVARCHAR(MAX) =DB_NAME()
/*设置备份的磁盘路径*/
SET @filename ='C:/'+ @dbname + '_'+ convert(nvarchar(200),replace(replace(replace(convert(nvarchar(200),getdate(),120),'-',''),':',''),' ',''))
PRINT @filename
SET @sqlstr= N' BACKUP DATABASE [' + @dbname
+N' ] TO DISK = ''' + @filename + N''''
+N' WITH COMPRESSION , NOUNLOAD, NAME = ''' + @dbname + N''''
+N' ,NOSKIP, STATS = 10, NOFORMAT '
/*动态执行的SQL 语句*/
PRINT @sqlstr
EXECUTE SP_EXECUTESQL @sqlstr