2016-07-27 99 views
0

我想獲得最大值MAX (ID)爲包含在我的DB 「Table_Example」和特定的一個模式名ID的每個表我對每個表(ID)。最大數據庫

單個例如:

SELECT MAX(ID) FROM Schema_name.Table_name1 

此檢索位於Table_name1最大ID值,但是我有84個表。我想知道每個表的最大值只在一列中。 這是我目前正在使用的代碼: 我正在使用information_schema.columns來自動獲取表的名稱以及每個表所屬的模式,以便將整個DB ID max(id)合併爲一個柱。

USE TABLE_EXAMPLE 
GO 
DECLARE @ID NVARCHAR(MAX) --int 

SET @ID = (SELECT DISTINCT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_SCHEMA = 'SCHEMA_NAME' AND COLUMN_NAME IN ('ID') AND DATA_TYPE = 'INT') 

SELECT @ID FROM (SELECT ('SCHEMA_NAME'+'.'+TABLE_NAME) AS TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_SCHEMA = 'SCHEMA_NAME' AND COLUMN_NAME = 'ID' AND DATA_TYPE='INT') AS W 

此腳本檢索錯誤的數據,但我認爲我是一個有點封閉,得到的值,但我不知道我做錯了。 有人可以給我任何好的方法嗎?或者有更好的選擇來完成它?

+0

是否在每個表中列名稱爲「ID」? –

+0

你只是試圖獲得每個表中的標識/索引列的最大值? –

+2

@JohnPasquet,但你設法發佈一個答案..給OP一些時間。並非所有人都在此時 –

回答

1

此枚舉所有表與此ID的列ID和MAX值:

DECLARE @query nvarchar(MAX); 
SELECT @query = COALESCE(@query + char(10)+'UNION ALL '+char(10)+'SELECT '''+QUOTENAME(s.name)+'.'+QUOTENAME(T.name)+''' [Table], MAX(Id) [Max] FROM '+QUOTENAME(s.name)+'.'+QUOTENAME(T.name), 
'SELECT '''+QUOTENAME(s.name)+'.'+QUOTENAME(T.name)+''' [Table], MAX(Id) [Max] FROM '+QUOTENAME(s.name)+'.'+QUOTENAME(T.name)) 
FROM sys.schemas S 
JOIN sys.tables T ON S.schema_id=T.schema_id 
JOIN sys.columns C ON T.object_id=C.object_id 
WHERE C.name='Id'; 
EXEC(@query); 
0

像這樣嘗試,

這會給你腳本。

SELECT DISTINCT 'SELECT MAX(' + + COLUMN_NAME + ') as ' + table_name + 'MaxId FROM ' + table_name 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_SCHEMA = 'dbo' 
    AND COLUMN_NAME IN ('ID') 
0

也許有點動態SQL

編輯這將返回表名(S)和最大ID在一個數據集

Declare @SQL varchar(max) = '>>>' 
Select @SQL = @SQL + SQL 
From (
     Select SQL='Union All Select TableName='''+concat('[',Table_Schema,'].[',Table_Name,']')+''',MaxID=max(ID) From '+concat('[',Table_Schema,'].[',Table_Name,'] ') 
      From INFORMATION_SCHEMA.COLUMNS 
      Where Column_Name = 'ID' 
    ) A 

Set @SQL=Replace(@SQL,'>>>Union All ','') 
Exec(@SQL) 
0

如果您想在身份列中使用最大值,而不考慮這些列的名稱,那麼這是一個非常簡單的方法。這會給你的表名,標識列的名稱,並且該列的最大值:

SELECT sys.tables.name AS [Table Name], 
    sys.identity_columns.name AS [Column Name], 
    last_value AS [Last Value]  
FROM sys.identity_columns 
    INNER JOIN sys.tables 
     ON sys.identity_columns.object_id = sys.tables.object_id 
ORDER BY last_value DESC 
0
CREATE TABLE #MaxValues (SchemaName SYSNAME , TableName SYSNAME , MaxID INT) 
GO 


Declare @SchemaName SYSNAME = 'dbo' --<-- Pass you schema name to this variable 
     ,@ColumnName SYSNAME = 'ID' --<-- Column Name 
     ,@DataType SYSNAME = 'INT' --<-- Data type 



DECLARE @TableName SYSNAME , @SchmaName SYSNAME 
     , @Sql NVARCHAR(MAX) , @ColName SYSNAME; 

Declare Cur CURSOR LOCAL FAST_FORWARD FOR 
SELECT s.name , t.name , c.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 
Inner join sys.types tp on tp.user_type_id = c.user_type_id 
WHERE s.name = @SchemaName 
AND c.name = @ColumnName 
AND tp.name = @DataType 

OPEN Cur 
    FETCH NEXT FROM Cur INTO @SchmaName , @TableName , @ColName 

WHILE (@@FETCH_STATUS =0) 
BEGIN 
    SET @Sql = N'INSERT INTO #MaxValues (SchemaName, TableName, MaxID)' 
      + N' SELECT @SchmaName ,@TableName, MAX(' + QUOTENAME(@ColName) + N') ' 
      + N' FROM ' + QUOTENAME(@SchmaName) + '.' + QUOTENAME(@TableName) 

    Exec sp_executesql @Sql 
         ,N'@SchmaName SYSNAME , @TableName SYSNAME' 
         ,@SchmaName 
         ,@TableName 
    FETCH NEXT FROM Cur INTO @SchmaName , @TableName , @ColName 
END 

CLOSE Cur 
DEALLOCATE Cur 

SELECT * FROM #MaxValues 
0

這個腳本會列出所有的最大的ID。假設你的第一列是ID,不管它的名字如何。

DECLARE @Script AS VARCHAR(MAX) = '' 

SELECT @Script = @Script + 'SELECT MAX(' + COLUMN_NAME + ') AS ID FROM ' + c.TABLE_NAME + ' UNION ALL ' + CHAR(13)+CHAR(10) 
FROM INFORMATION_SCHEMA.COLUMNS c 
INNER JOIN INFORMATION_SCHEMA.TABLES t ON c.TABLE_NAME = t.TABLE_NAME 
WHERE c.ORDINAL_POSITION = 1 and t.TABLE_TYPE = 'BASE TABLE' and c.TABLE_SCHEMA = 'dbo' and c.DATA_TYPE = 'int' 

SELECT @Script = LEFT(@Script, LEN(@Script) - 12) 

EXEC (@Script)