2017-08-24 68 views
3

我有查詢,列出在我的數據庫中的所有表各自的指標:SQL遞歸指數調查結果查詢

SELECT 
    TableName = t.name, 
    IndexName = ind.name, 
    IndexId = ind.index_id, 
    ColumnId = ic.index_column_id, 
    ColumnName = col.name, 
    ind.*, 
    ic.*, 
    col.* 
FROM 
    sys.indexes ind 
INNER JOIN 
    sys.index_columns ic ON ind.object_id = ic.object_id and ind.index_id = ic.index_id 
INNER JOIN 
    sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id 
INNER JOIN 
    sys.tables t ON ind.object_id = t.object_id 
WHERE 
    ind.is_primary_key = 0 
    AND ind.is_unique = 0 
    AND ind.is_unique_constraint = 0 
    AND t.is_ms_shipped = 0 

我得到的輸出是這樣的:

enter image description here

TableName -> contain Table names in DB ColumName -> contain Index column name for each table in DB

我的目標是繪製備案和最後一個(最大)的ColumnName值的每"TableName" + "ColumnName" TOT數。

基本上就在標籤格式輸出,我想獲得: enter image description here

你有任何想法如何遞歸地產生這樣的結果在一個單一的查詢? 謝謝!

+1

*「爲每個繪圖」TableName「+」ColumnName「記錄的TOT編號和最後一個(最大)ColumnName值。」*:我不知道這意味着什麼。你可以在你的問題中包含表格,因爲你希望輸出是? – trincot

+0

剛剛添加。基本上從主查詢,我需要每各表名以及每個的ColumnName迭代並獲得記錄和最後一個值的TOT數。 – user3925023

+0

@ user3925023 - 每個表都有主鍵? – Poonam

回答

1

好,這可能是速度不夠快,在你的安裝運行,是我最好的嘗試。有涉及列和max()骨料的類型,這決定了max_value應該是一個varchar問題。

只要確保你use your_database_name創建和執行這個程序,這對於應用程序調用類似於單查詢之前。

create proc stp_generateColInfo as 

declare 
@tablename varchar(1000), 
@columnname varchar(1000) 

set nocount on 

create table #TMP_DDL (
    TableName varchar(1000), 
    ColumnName varchar(1000), 
    TOTAL int, 
    MAX_VALUE varchar(8000) 
); 

INSERT INTO #TMP_DDL 
SELECT 
    TableName = t.name, 
    ColumnName = col.name, 
    0 as TOTAL, 
    '' as MAX_VALUE 
FROM 
    sys.indexes ind 
INNER JOIN 
    sys.index_columns ic ON ind.object_id = ic.object_id and ind.index_id = ic.index_id 
INNER JOIN 
    sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id 
INNER JOIN 
    sys.tables t ON ind.object_id = t.object_id 
WHERE 
    ind.is_primary_key = 0 
    AND ind.is_unique = 0 
    AND ind.is_unique_constraint = 0 
    AND t.is_ms_shipped = 0 


declare cs cursor local static forward_only for 
    select TableName, ColumnName from #TMP_DDL 

open cs 
fetch next from cs into @tablename, @columnname 
while @@FETCH_STATUS=0 
begin 
    exec(' 
    declare @total int, @max_value varchar(8000) 
    select @total=count(*), @max_value=max(IsNull(' + @columnname + ',0)) from ' + @tablename + ' 
    update #TMP_DDL set [email protected], [email protected]_value where TableName=''' + @tablename + ''' and ColumnName=''' + @columnname + '''' 
    ) 
    fetch next from cs into @tablename, @columnname 
end 

close cs 
deallocate cs 

set nocount off 

--Add joins here to select additional columns :) 
select * from #TMP_DDL 

go 
+0

有沒有辦法避免創建表?我沒有權利這樣做.Thx – user3925023

+1

請參閱:https://jasonstrate.com/2013/05/21/security-questions-what-permissions-are-需要創建臨時表/我認爲每個用戶都有權創建臨時表! – Niloct

1

你有任何想法如何遞歸地產生這樣的結果在 單個查詢?

這是不可能的。

您需要單獨調用動態SQL語句

SET @sql = 'SELECT @MaxID = MAX('[email protected]+') FROM '[email protected] 

您的結果集的每一行和functions cannot contain dynamic SQL這是不可能把它寫在SQL Server的一個select語句。您不能單獨調用動態SQL或a stored procedure爲每一個行中的一個查詢。

在Oracle中有像BULK COLLECT and FORALL這樣的命令允許將結果集與逐行處理結合起來,但我不知道SQL Server中存在類似的東西。