create proc backup_db @targetPath nvarchar(50)
as
set nocount on
if @targetPath=''
return ;
declare @sql nvarchar(max)
set @targetPath=@targetPath+case when RIGHT(@targetPath,1)='\' then '' else '\' end
set @sql=''
select @sql=@sql+'backup database ['+name+'] to disk='''+@targetPath+name+'.bak'' '+CHAR(10)
from sys.databases
where name not in('master','tempdb','model','msdb')
--print @sql
exec (@sql)
go