Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
  • 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)

share|improve this question
for example: if version = 01 run scripts 3,4,5 | if version = 02 run scripts 6,7,8 – Gilad Bronshtein Nov 30 '12 at 18:28

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)
if (Select Coalesce(Max([Version]),0) from VersionInfo) = 0
      Print 'Your first Script'      
      EXEC('Create Table test1 (ID int)') 
      Insert into Test1 Values (1)
      insert into VersionInfo Values (GetDate(),1,'Version 1')
if (Select Coalesce(Max([Version]),0) from VersionInfo) = 1
      Print 'Your second Script'      
      EXEC('Create Table test2(ID int)') 
      insert into VersionInfo Values (GetDate(),2,'Version 2')
if (Select Coalesce(Max([Version]),0) from VersionInfo) = 2
      Print 'Your third Script'     
      Insert into Test1 Values (1)
      insert into VersionInfo Values (GetDate(),3,'Version 3')
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .