SQL Statement, alle tabellennamen incl. rowcount anzeigen

... komplette Frage anzeigen

1 Antwort

    SET NOCOUNT ON;          

CREATE TABLE #TestTable (Table_Name varchar(255) not null, AnzahlReihen int null)    

DECLARE @TabellenCursor CURSOR  
DECLARE @Klartext varchar(255)  
DECLARE @Fetch_Klartext int
DECLARE @STRSQL as varchar(255)


SET @TabellenCursor = CURSOR FOR 
SELECT Table_Name FROM INFORMATION_SCHEMA.TABLES WHERE Table_Name LIKE 'TBL%2012%' ORDER BY TABLE_NAME


OPEN @TabellenCursor    
FETCH NEXT FROM @TabellenCursor INTO @Klartext    
SELECT @Fetch_Klartext = @@FETCH_STATUS    
WHILE (@Fetch_Klartext = 0)  BEGIN    
SET @STRSQL ='INSERT INTO #TestTable (Table_Name, AnzahlReihen) SELECT ''' + @Klartext + ''', (SELECT COUNT (*) FROM ' + @Klartext + ')'
EXEC(@STRSQL)   
FETCH NEXT FROM @TabellenCursor INTO @Klartext   
SELECT @Fetch_Klartext = @@FETCH_STATUS  END     
CLOSE @TabellenCursor      

SELECT * FROM #TestTable 
Antwort bewerten Vielen Dank für Deine Bewertung
Kommentar von Lethargor
28.03.2012, 11:37

...da muss noch ein

DROP TABLE #TestTable

dahinter...

0

Was möchtest Du wissen?