OBTENER MAX (ID) PARA TODAS LAS TABLAS SERVIDOR SQL

I am trying to select Max(ID) for all tables in my database. I tried the below query but there is no AUTO-INCREMENT column available. Is there something else that I can try?

SELECT TABLE_NAME, AUTO_INCREMENT
FROM   information_schema.TABLES
WHERE  TABLE_SCHEMA = 'mydb'

preguntado el 09 de septiembre de 13 a las 22:09

Are you talking about the maximum Object_id? -

2 Respuestas

Are you trying to get the max value for each IDENTITY column in your DB? that is possible by modifying the script in a previous answer to examine the is_identity column on the sys.columns table.

 CREATE TABLE #x(t NVARCHAR(520), c BIGINT);

DECLARE @sql NVARCHAR(MAX);

SET @sql = N'';

SELECT @sql = @sql + N'INSERT #x SELECT ''' 
  + QUOTENAME(s.name) + '.' + QUOTENAME(t.name) + ''',
  MAX(' + c.name + ') FROM '
  + QUOTENAME(s.name) + '.' + QUOTENAME(t.name) + ';'

FROM sys.columns C
    INNER JOIN sys.tables T ON C.object_id = T.object_id
    INNER JOIN sys.schemas s ON S.schema_id = T.schema_id
WHERE is_identity = 1;

EXEC sp_executesql @sql;

SELECT t, c FROM #x;

DROP TABLE #x;

Respondido el 09 de Septiembre de 13 a las 23:09

CREATE TABLE #x(t NVARCHAR(520), c BIGINT);

DECLARE @sql NVARCHAR(MAX);

SET @sql = N'';

SELECT @sql = @sql + N'INSERT #x SELECT ''' 
  + QUOTENAME(s.name) + '.' + QUOTENAME(t.name) + ''',
  MAX(ID) FROM '
  + QUOTENAME(s.name) + '.' + QUOTENAME(t.name) + ';'
FROM sys.tables AS t
INNER JOIN sys.schemas AS s
ON t.[schema_id] = s.[schema_id]
INNER JOIN sys.columns AS c
ON t.[object_id] = c.[object_id]
WHERE c.name = N'ID';

EXEC sp_executesql @sql;

SELECT t, c FROM #x;

DROP TABLE #x;

Respondido el 09 de Septiembre de 13 a las 22:09

I get the following error: Operand type clash: uniqueidentifier is incompatible with bigint - Klay

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.