Running a query with conditions

0

  • i have a system with dew version. any version has 5-9 upgrade scripts. in order to upgrade version 1 to 5 i have to run each script in any version in the correct order (01_script, 02_script, 03_script...) if the installed version is 01, i start running to script from version 02 i have table with the value of the version number that also written in registry.

i have the ability to take all script and union them into one script

how can i make a script which will check the version and start running the correct script?

(sql server 2008r2)

Gilad Bronshtein

Posted 2012-11-30T18:27:16.883

Reputation: 1

1for example: if version = 01 run scripts 3,4,5 | if version = 02 run scripts 6,7,8 – Gilad Bronshtein – 2012-11-30T18:28:12.047

Answers

0

it's possible, but not always easy. As a trunk:

if not exists (select * from sysobjects where name='VersionInfo' and xtype='U')
    create table VersionInfo (InstDate dateTime,[Version] int, Name varchar(64) not null)
GO    
if (Select Coalesce(Max([Version]),0) from VersionInfo) = 0
    begin   
      Print 'Your first Script'      
      EXEC('Create Table test1 (ID int)') 
      Insert into Test1 Values (1)
      insert into VersionInfo Values (GetDate(),1,'Version 1')
    end
if (Select Coalesce(Max([Version]),0) from VersionInfo) = 1
    begin   
      Print 'Your second Script'      
      EXEC('Create Table test2(ID int)') 
      insert into VersionInfo Values (GetDate(),2,'Version 2')
    end    
if (Select Coalesce(Max([Version]),0) from VersionInfo) = 2
    begin   
      Print 'Your third Script'     
      Insert into Test1 Values (1)
      insert into VersionInfo Values (GetDate(),3,'Version 3')
    end  

bummi

Posted 2012-11-30T18:27:16.883

Reputation: 1 569