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