EXECUTE dbo.VX_UPGRADE_CLEAR
GO

-- Установка прав на хранимые процедуры
exec GRANT_PERM 'P'
GO
exec GRANT_PERM 'U'
GO
exec GRANT_PERM 'V'
GO
exec GRANT_PERM 'TF'
GO
exec GRANT_PERM 'IF'
GO
exec GRANT_PERM 'FN'
GO
delete from dbo.s_dbobjects where object_id(DBObject) is null
GO
UPDATE dbo.S_PERMISSIONS SET Permitted = Permitted, Restricted = Restricted
GO
----Проверка на процессы
--Declare processes cursor LOCAL STATIC READ_ONLY FOR
--	select 'kill '+RTRIM(str(spid)) from master.dbo.sysprocesses where dbid = db_id() and spid <> @@SPID AND IS_SRVROLEMEMBER('sysadmin') = 1 AND CMD <> 'CHECKPOINT'
--Declare @spid varchar(30), @num int
--SET @num = 1000
--Open processes
--Fetch next from processes into @spid
--WHILE @@FETCH_STATUS = 0 AND @num > 0
--BEGIN
--	EXECUTE(@spid)
--	SET @num = @num - 1
--	Fetch next from processes into @spid
--END
--Close processes
--Deallocate processes
--GO

DECLARE @SQL VARCHAR(1000)
IF LEFT(CONVERT(VARCHAR(100),SERVERPROPERTY(N'ProductVersion')),2) <> '11'
BEGIN
	SET @SQL = '
	Declare @dbname sysname
	Select @dbname = db_name()
	exec sp_dboption @dbname, ''dbo use only'', ''false''
	'
	EXECUTE (@SQL)
END
GO

Declare @maj varchar(10), @min varchar(10), @rel varchar(10), @fix varchar(10), @ver varchar(100)
SELECT 
	@maj = convert(varchar(10), Release / 65536),
	@min = convert(varchar(10), (Release / 256) % 256),
	@rel = convert(varchar(10), Release % 256),
	@fix = convert(varchar(10), fix)
from dbo.s_release where release = (select top 1 release from dbo.s_release order by release desc, fix desc)

SET @ver = RTRIM(@maj) + '.' + RTRIM(@min)
EXECUTE dbo.X_SET_PROP 'PROJECT', 'VERSION', @ver
EXECUTE dbo.X_SET_PROP 'PROJECT', 'VERSION_MAJOR', @maj
EXECUTE dbo.X_SET_PROP 'PROJECT', 'VERSION_MINOR', @min
EXECUTE dbo.X_SET_PROP 'PROJECT', 'VERSION_RELEASE', @rel
EXECUTE dbo.X_SET_PROP 'PROJECT', 'VERSION_FIX', @fix

DECLARE @LAST_AUTOUPDATE VARCHAR(30), @LAST_VERSION VARCHAR(30)
SET @LAST_AUTOUPDATE = convert(varchar(10),GETDATE(), 104) + SPACE(1) +  convert(varchar(15),GETDATE(), 114)
SET @LAST_VERSION = RTRIM(@maj) + '.' + RIGHT('0' + RTRIM(@min),2) + 'SP' + @fix

--Такая строчка нужна, чтобы правильно формировалась статистика
SET @ver = 'Установка SP №' + @LAST_VERSION
EXECUTE WF_GENERATEEVENT 'FIX', @ver

EXEC X_SET_OPTION 'LAST_AUTOUPDATE', 0, @LAST_AUTOUPDATE
EXEC X_SET_OPTION 'LAST_VERSION', 0, @LAST_VERSION

GO
WHILE @@TRANCOUNT > 0
	raiserror('@@TRANCOUNT>0!!!',11,1)
GO
Declare @kassa int, @BB datetime, @BE datetime
Select @kassa = LINK from S_OBJECTS Where CODE = 'SMETA'
SELECT @BB = BEGINDATE, @BE = ENDDATE from tmp_dates2
IF @BB IS NULL OR @BE IS NULL
BEGIN
	SELECT @BB = CONVERT (DATETIME, CONVERT (VARCHAR (8), GETDATE (), 112), 112)
	SELECT @BE = @BB
END
EXECUTE dbo.I_SET_ADMIN_DATE @kassa, @BB, @BB, @BE
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[tmp_dates2]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [tmp_dates2]
GO
WHILE @@TRANCOUNT > 0
	raiserror('@@TRANCOUNT>0!!!',11,1)
GO
