Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 number of rows in a table

Author  Topic 

venkatch786
Starting Member

8 Posts

Posted - 2010-06-30 : 03:15:05
Hi,
I want to retrieve data like column names with total number of rows in a table.

Thanks,
Venkat

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-06-30 : 03:19:19
What do you mean??
Each column in a table has the same number of rows in a table.
Can you be more specific and give an example?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-06-30 : 03:42:45
[code]DECLARE @Tables TABLE
(
TableName SYSNAME PRIMARY KEY CLUSTERED,
Records BIGINT NOT NULL
)

DECLARE @SQL NVARCHAR(MAX)

SET @SQL = (
SELECT 'SELECT ' + QUOTENAME(TABLE_SCHEMA + '.' + TABLE_NAME, '''')
+ ' AS TableName, COUNT_BIG(*) AS Records FROM ' + QUOTENAME(TABLE_SCHEMA)
+ '.' + QUOTENAME(TABLE_NAME) + ';'
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'Core'
FOR XML PATH('')
)

INSERT @Tables
(
TableName,
Records
)
EXEC (@SQL)

SELECT t.TableName,
t.Records,
STUFF(f.ColumnNames, 1, 1, '') AS ColumnNames
FROM @Tables AS t
CROSS APPLY (
SELECT ',' + c.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS AS c
WHERE c.TABLE_SCHEMA + '.' + c.TABLE_NAME = t.TableName
FOR XML PATH('')
) AS f(ColumnNames)[/code]


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -