2012-11-23 121 views
1

我必須編寫一個能夠使用表version將數據庫引入類型中的特定時刻的過程。例如從版本1移動到版本10或其他方式。事情是這一章我很模糊,學校課程幾乎沒有關於它。我試圖使用互聯網來建立一個解決方案,但不知何故,我卡住了。請幫我理解我做錯了什麼。數據庫版本

表版本,1個columnm,int類型

查詢

create procedure [dbo].[goto_vs] (
     @vs int 
    ) 
    as 
    begin 
     declare @current_vs int, @counter int; 
     declare @sqlquery nvarchar(50); --query to modify 
     declare @sqlsp nvarchar(30); 
     declare @sqlversion nvarchar(3); 
     declare @sqlreverse nvarchar(10); 

     --get the current version from table 
     select @current_vs=version from dbo.version; 

     --checking for valid version 
     if (@current_vs = @vs) begin 
      print('The database is already at this version...') 
      return 
     end 
     else begin 
      if (@vs > 5) begin 
       print('Setting the version of databse to last one...') 
       set @vs = 5 
      end 
      else begin 
       if (@vs < 0) begin 
        print('Setting the database to default...') 
        set @vs = 0 
       end 
      end 
     end 

     --setting up the string for exec 
     set @sqlsp = 'exec sp_create_table_awards' 

     --check if we go further or earlier in time 
     print('Changing database version...') 
     if (@vs > @current_vs) begin 
      set @sqlreverse = '' 
      goto upgrading 
     end 
     else begin 
      set @sqlreverse = 'undo_create_awards' 
      goto downgrading 
     end 

     --upgrading code 
     upgrading: 
      set @counter = @current_vs + 1 
      while (@counter <= @vs) begin 
       set @sqlquery = @sqlsp + cast(@counter as nvarchar(2)) + @sqlreverse 
       print(@sqlquery) 
       exec sp_executeSql @sqlquery 
       set @counter = @counter + 1 
      end 
      goto ending 
     downgrading: 
      set @counter = @current_vs 
      while (@counter > @vs) begin 
       set @sqlquery = @sqlsp + cast(@counter as nvarchar(2)) + @sqlreverse 
       print(@sqlquery) 
       exec sp_executeSql @sqlquery 
       set @counter = @counter - 1 
      end 
      goto ending 
     ending: 
      update dbo.version set [email protected] 
      print('Database version changed...') 
    end 

回答

1

考慮到我想出一個辦法,並沒有得到答覆,我將它張貼作爲響應,因爲它可以幫助其他的學生這studie計算機科學

爲了簡化我叫我用這種模式do_xundo_x其中x是INT在哪裏/取消所有程序_x是程序,這對toughter例如DO_ 1和undo_1創建表格並銷燬表格

ALTER PROCEDURE [dbo].[goto_vs] 
    -- Add the parameters for the stored procedure here 
    @v int = 0 

AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET NOCOUNT ON; 

    -- Insert statements for procedure here 
    declare @i int 
    declare @toexec nvarchar(70) 

    set @i = (select version from version) 

    -- If the requested version is lower than the current one, downgrade. 
    while(@i > @v) 
     BEGIN 
      set @i = @i - 1 
      set @toexec = 'Undo_' + CONVERT(varchar,@i); 
      exec sp_executeSql @toexec 
      print @toexec 
     END 

    -- Otherwise, upgrade. 
    while(@i < @v) 
     BEGIN 
      set @toexec = 'Update_' + CONVERT(varchar, @i); 
      exec sp_executeSql @toexec 
      set @i = @i + 1 
      print @toexec 
     END 
END