我必须编写一个程序,该程序能够使用表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