Bases de datos

SQL Server

Consulta dinámica de cantidad de registros por tabla

Generación dinámica de SELECTs de conteo de registros de todas las tablas de la base de datos a la que se está conectado.

 
SELECT CASE WHEN NROW = 1 THEN '' ELSE ' union ' END + XSQL AS XSQL
FROM (
SELECT ROW_NUMBER() OVER(ORDER BY a.name, b.name ASC) AS NROW,
	'select ''' +  b.name + ''' as TABLE_SCHEMA, ''' + a.name + ''' as TABLE_NAME, count(*) as NROWS'
	+ ' from [' +  b.name + '].[' + a.name + ']' AS XSQL
FROM sys.objects a inner join sys.schemas b ON b.schema_id = a.schema_id 
WHERE a.TYPE = 'u'
) AS x