数据库版本

时间:2012-11-23 10:56:56

标签: sql sql-server

我必须编写一个程序,该程序能够使用表version将数据库带入类型中的特定时刻。例如,从版本1移动到版本10或反过来。问题是我对本章非常模糊,而且学校课程几乎没有任何关于它的内容。我尝试使用互联网来构建一个解决方案,但不知怎的,我卡住了。请帮我理解我做错了什么。

表格版本,1列,输入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 version=@vs
            print('Database version changed...')
    end

1 个答案:

答案 0 :(得分:1)

考虑到我找到了方法,并且没有得到任何回复,我会将其作为回复发布,因为它可以帮助其他学生学习计算机科学

为了简化,我按照这种模式do_xundo_x命名了我的所有程序,其中x是一个int,其中do / undo _x是对比较强的程序,例如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