0

I've got the task to automate the database modification scripts in our simulation server.

Each simulation generates a few databases, all based from an common base DB, which is empty.

But now is needed to do the same to get the data from last year's simulation database.

I devised to create an clone of that DB based on a backup from the moment we need. And apply all database changes from the moment the DB was created till now.

So the mechanism I imagine is something that can run an script (like the ones generated either by ER/Studio or SSMS) stored in a version table - which will be called from a SQL Agent job step.

EDIT: What is the simplest way to do that - running an script stored in a table from an SQL Agent job step?

P.S.: if the solution can avoid xp_cmdShell or sql_cmd or even preprocessing the script either in Delphi or in a SQLCLR function, it's better - but if's the only way I can cope with that.

Fabricio Araujo
  • 3,810
  • 3
  • 28
  • 43

1 Answers1

0

You can run a proc that does this:

create table jobs (step varchar(max))
insert into jobs  values ('select * from sys.objects')
insert into jobs values ('select * from sys.databases')
--create a proc from this code
declare @curjob nvarchar(max)
declare @prevjob nvarchar(max)
select top 1 @curjob= step from jobs order by step
while @curjob is not null
    begin
        exec sp_executesql @curjob
        set @prevjob=@curjob 


select top 1 @curjob = step from jobs 
         where step > @prevjob        
         order by step
   if @@ROWCOUNT =0
                break

        end
benjamin moskovits
  • 5,261
  • 1
  • 12
  • 22