|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Hi everyone this is my first post (ever on the net, haven’t really used forums before). Well my problem is simple (well not to me but to someone out there)
I need to count the number or rows in every table in a Microsoft SQL database. Displaying something like Table No. of rows table1 125 table2 654 table3 5757 table4 455 etc... Any help would be really appreciated Thanks everyone :-) |
|
#2
|
|||
|
|||
|
with Entreprise Manager, there is a tab where you all see the Info but if you need it in SQL
try this: EXEC sp_spaceused '<your table>' and you can see the number of row,DB space used,index space used /per table (you can do a loop do get all the tables you need, or just call sp_spaceused X times) ![]() |
|
#3
|
|||
|
|||
|
Hey Thanks for the reply,
still have a problem, I need to be able to call count() on all tables but don’t want to hard code in all the table names, really what I want is to “select count(*) from * ” this doesn’t work obviously but is there some way of getting all the table names and then do a loop passing each table name, or something, Again guys oh and girls THANKS |
|
#4
|
|||
|
|||
|
This will return a single-row resultset for each table in your database:
exec sp_MSforeachtable 'select count(*) as nr_of_rows, ''?'' as table_name from ?'
__________________
Martijn Tonies Database Workbench: developer IDE for Firebird, MySQL, InterBase, MSSQL Server and Oracle Upscene Productions http://www.upscene.com |
|
#5
|
|||
|
|||
|
HEY THANK YOU SO SO SO MUCH, Works perfectly!!! have been working on it for ages, using temp tables, variable, cursers etc. tried everywhing..... Thanks Again!!!!
![]() |
|
#6
|
|||
|
|||
|
upscene...which version of SQL has your sp_MSforeachtable? cuz I don't have it and I got SQL2000...no SP thought...
tjbyme: if you want to have all the table in your DB try this select name from sysobject where (type='u') this will get you all the tables and what you need is a loop to call the sp_spaceused wiht the table name |
|
#7
|
|||
|
|||
|
Using MSSQL 2000 - it's in the "master" database for a standard install.
|
|
#8
|
|||
|
|||
|
Hey Sypher
the Code:
exec sp_MSforeachtable Code:
create table temp_tab ( tab_name varchar(30), no_of_rows INTEGER, ) DECLARE curREVIEW CURSOR FOR select name from sysobjects where xtype = 'U' DECLARE @cur_tab_name varchar(30) OPEN curREVIEW FETCH curREVIEW INTO @cur_tab_name WHILE (@@FETCH_STATUS =0) BEGIN DECLARE @count integer select @count = count(*) from @cur_tab_name INSERT INTO temp_tab (@cur_tab_name, @count) FETCH curREVIEW INTO @cur_tab_name END CLOSE curREVIEW DEALLOCATE curREVIEW |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Count All Rows In All Tables In Db |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|