I have to write a procedure which is able using a Table version to bring the database to a specific moment in type. For instance to move from version 1 to version 10 or the other way around. The thing is I’m pretty blurry with this chapter, and the school course has almost nothing about it. I tried using the internet to build a solution but somehow I got stuck. Please help me understand what am I doing wrong.
Table version, 1 columnm, type int
query
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
Considering I figured a way, and have got no responses, I will post it as a response because it may help other students which studie computer science
To simplify I named all my procedures by this pattern
do_xandundo_xwherexis a int where do / undo _x are procedures which pair toughter for example do_1 and undo_1 create a table and destroy a table