Scripts sql (maintenance)

De Wiki1000

RootOID

Retourne le max des racines d'OID d'une base de données avec OID caractères.

DECLARE @SQL nvarchar(max)
DECLARE @TableName sysname
DECLARE @TableUID int
DECLARE @RootOID int

DECLARE @MaxUID int
DECLARE @MaxTableName sysname

SELECT @MaxUID = 0
SELECT @RootOID = cValue from sysCounter where cName='RootOID'

DECLARE tables_cursor CURSOR FAST_FORWARD
FOR
select t.name from sys.all_columns c
join sys.tables t on t.object_id = c.object_id
where c.name='oid'

OPEN tables_cursor

FETCH NEXT FROM tables_cursor INTO @TableName

WHILE @@FETCH_STATUS = 0
BEGIN 
  SELECT @SQL = 'select @TableUID = MAX(convert(INT,  CONVERT(VARBINARY,''0x''+SUBSTRING(oid,21,8),1) )) from '+ @TableName
  EXEC sp_executesql  @SQL, N'@TableUID int OUTPUT, @TableName varchar', @TableUID OUTPUT, @TableName = @TableName

  IF (@TableUID > @MaxUID ) 
   BEGIN 
     SELECT @MaxUID=@TableUID
	 SELECT @MaxTableName = @TableName
   END

  FETCH NEXT FROM tables_cursor INTO @TableName

END

SELECT @MaxTableName, @MaxUID, @RootOID

CLOSE tables_cursor
DEALLOCATE tables_cursor
Tip-20px.png Tip : La valeur de RootOID est stockée dans la séquence "RootOID" de la base
Outils personnels