| Author |
Topic |
|
itsmarkdavies
Starting Member
14 Posts |
Posted - 2006-03-08 : 09:20:43
|
| Can anyone give me some T-SQL to do an insert into a table of all the TableNames and RecordCounts of ALL User Tables in a given SQL Server Database please ?What I want is something like this :-Table1 0Table2 12Table3 356Table4 0....etcI have managed to do it with a Cursor but it is a bit untidy and slow, and I am sure someone will have something better.Thanks,mark.davies@npower.com |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-03-08 : 09:33:50
|
| This is more or less give you correct resultselect s.name,i.rows from sysobjects s inner join sysindexes ion s.id=i.id where i.indid<2 and s.xtype='u' order by s.nameMadhivananFailing to plan is Planning to fail |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-03-08 : 09:39:58
|
you could build a dynamic sql from information_schema views and the run it.or run this in your db:select distinct o.name, i.rows from sysindexes i join sysobjects o on i.id = o.idwhere o.xtype = 'U' and o.name <> 'dtproperties' and i.indid < 2 this need your statistics to be up to date.Go with the flow & have fun! Else fight the flow |
 |
|
|
itsmarkdavies
Starting Member
14 Posts |
Posted - 2006-03-08 : 09:40:15
|
| Thanks Madhivanan, I was aware of this method but I need to be exact and this does not always return the correct results. |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-03-08 : 09:41:05
|
Go with the flow & have fun! Else fight the flow |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-03-08 : 09:50:14
|
quote: Originally posted by itsmarkdavies Thanks Madhivanan, I was aware of this method but I need to be exact and this does not always return the correct results.
Then you should use Select count(*) MadhivananFailing to plan is Planning to fail |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2006-03-08 : 09:55:54
|
| Feel free to use a cursor to loop through the names of the tables in your database. But definitely don't use a cursor to calculate the row counts! just declare your cursor, loop through table names, SELECT COUNT(*) from each, and off you go.The cursor will in no way be a bottleneck for your performance if used this way. |
 |
|
|
itsmarkdavies
Starting Member
14 Posts |
Posted - 2006-03-08 : 10:05:55
|
| Thanks for that Dr. Cross Join. I've sussed it now. See code below for anyone interested :-CREATE TABLE #TablesList(Server_Name VARCHAR(10), Table_Name VARCHAR(50), Record_Count NUMERIC(15, 0))DECLARE @TableName VARCHAR(50)DECLARE MyTables CURSOR FOR SELECT LTrim(RTrim(CONVERT(CHAR(50), OBJ.[Name]))) FROM sysobjects OBJ WHERE OBJ.Type = 'U' ORDER BY OBJ.[Name] ASCOPEN MyTablesFETCH NEXT FROM MyTables INTO @TableNameWHILE @@FETCH_STATUS = 0BEGIN EXEC ('INSERT #TablesList SELECT @@SERVERNAME, ''' + @TableName + ''', COUNT(*) FROM ' + @TableName) FETCH NEXT FROM MyTables INTO @TableNameENDCLOSE MyTablesDEALLOCATE MyTablesSELECT * FROM #TablesListDROP TABLE #TablesList |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-03-08 : 18:12:25
|
Non cursor solution declare @sql nvarchar(500)create table #TablesList(DataBase_Name varchar(10), Table_Name varchar(50), Record_Count NUMERIC(15, 0))select @sql = 'select ''insert into #TablesList select db_name(), '''''' + name + '''''', count(*) from '' + name from sysobjects where type = ''U'''print @sqlexec master..xp_execresultset @sql, N'test'select * from #TablesList order by DataBase_Name, Table_Namedrop table #TablesList ----------------------------------'KH' |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2006-03-08 : 18:39:31
|
| just keep in mind that the "non-cursor solution" gives you absolutely no advantage over the cursor solution. |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-03-09 : 05:08:50
|
i guess that this falls into that 1% when cursors are ok Go with the flow & have fun! Else fight the flow |
 |
|
|
|