SQL Server – Generate SQL for Database Snapshot

with
preamble(c) as (select 'create database ' + db_name() + '_Snapshot on'),
files(c) as (select '(name=' + name + ', filename=''' + physical_name + '.ss'')' + char(10) from sys.database_files where type = 0),
filescoalesce (c) as (select c + ',' from files for xml path('')),
lastline(c) as (select 'as snapshot of ' + db_name() + char(10) + char(10) + 'GO' + char(10))
select c [--] from preamble
union all
select left(c, len(c) -2) from filescoalesce
union all
select c from lastline

Leave a Reply

Your email address will not be published. Required fields are marked *