views:

46

answers:

1

How can i calculate the size of only some rows for each table?

For example, with the code:

EXEC sp_spaceused 'myTable'

you obtain the size of all rows, however i want to calculate the size not of one single table, but all of them, and using the same clause for each one, something like this in pseudo-code:

foreach(Table myTable in Database)
  EXEC sp_spaceused 'myTable WHERE AppId='abc''

How can i achieve this with T-SQL?

Thanks in advance

EDIT: for better clarification

+3  A: 
-- Plop the data into a temp table
Select myFields into #tmpdata from myTable where myCondition = 'foo'

use tempdb
GO
exec sp_spaceused #tmpdata
souLTower
Good idea. I'd probably use "Select myFields into tmpRealTable..." though, and drop it after, but no difference
gbn
Thanks for the answer, its a good idea, but for one table only. However, as i clarified in the question, i want to do this in all tables. Creating temp tables for all of them would be a big script..is there a way to do it with a cycle or something?
Tom S.
Also, i tried that now in SSMS and gives me this error: "The object '#tmpdata' does not exist in database 'myDb' or is invalid for this operation."But if i do "select * from #tmpdata", gives normal results. So i wonder that this solution doesnt works.
Tom S.
It's possible. You would create a script that dynamically stores the tables you want (look at joining the sysobjects table to the syscolumns table), then iterate over that table using a loop. Don't use cursors. With some forethought this could be a handy set of admin tools.ST
souLTower