Record per tabella.

lunedì 25 luglio 2011 - 13.54
Tag Elenco Tags  SQL Server 2008 R2

iif Profilo | Expert

Ciao, vorrei conoscere il numero dei record per ogni tabella presente in un database, vi posto il codice:

USE [databeis]
GO
/****** Object: StoredProcedure [dbo].[QueryImpatto_SP] Script Date: 07/25/2011 13:46:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[QueryImpatto_SP]
AS

DECLARE @nometabella nvarchar(50);

DECLARE Cursore CURSOR FOR
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'

OPEN Cursore;
FETCH NEXT FROM Cursore; --INTO @nometabella;

--WHILE @@FETCH_STATUS = 0
--BEGIN

SELECT + @nometabella as Tabella , COUNT(*) as Quanti FROM + @nometabella;

--END
CLOSE Cursore;

DEALLOCATE Cursore;
------------------------------------------------
Dimentico qualcosa?

Grazie.

boccia75 Profilo | Junior Member

Ti do' questa che utlizzo io ed è senza l'uso di cursori:

SELECT a.name "Tabella", b.rows "Record"
FROM sysobjects a, sysindexes b
WHERE a.id = b.id AND b.indid <= 1 AND a.type = 'U'
AND a.name <> 'dtproperties'
ORDER BY a.name

Ciao,
Luca

iif Profilo | Expert

Grazie

iif Profilo | Expert

Grazie, e se volessi avere anche gli indici?

boccia75 Profilo | Junior Member

Adesso controllo.
Eccomi, questa è la query:

SELECT getdate(),b.name,i.name,I.TYPE_DESC
from sys.indexes i,sys.tables b
where B.object_id = i.object_id
and I.TYPE_DESC<>'HEAP'

Ciao
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5