Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all , I will select some tablesNames with the below code .

SQL
SELECT sobjects.name
FROM sysobjects sobjects
WHERE sobjects.xtype = 'U' and SUBSTRING(sobjects.name,0,10) = 'MyFirstTenCharacter' 


SQL
name
r_Sales05
r_Sales05_CombinedRequests
r_Sales05_NotOrdered
r_Sales05_NotRequested
r_Sales05_SplitRequests
r_Sales05_UnmatchedAmounts
r_Sales05_UnmatchedCustomers
r_Sales05_UnmatchedItemNumbers
r_Sales05_UnmatchedQuantities
r_Sales05_UnmatchedYears


It will show me 11 TableNames . now i would like to select Count(Amount) of those table's in front of them with a join . Something like below .


VB
name                                              Amount
r_Sales05                                         0
r_Sales05_CombinedRequests                        100  
r_Sales05_NotOrdered                              1000
r_Sales05_NotRequested                            900000
r_Sales05_SplitRequests                           3
r_Sales05_UnmatchedAmounts                        5
r_Sales05_UnmatchedCustomers                      0
r_Sales05_UnmatchedItemNumbers                    12
r_Sales05_UnmatchedQuantities                     100000 
r_Sales05_UnmatchedYears                          0


Note.
I don't want rowCount , each of 10 tables has an amount fields . I would like to show the sum of amounts of each table in the other col .

Thanks .
Posted
Updated 6-Dec-14 2:30am
v2

1 solution

First, you need 1,10in your SUBSTRING, not 0,10 - SQL indexes start at 1.

SQL
SELECT sobjects.name, st.row_count
FROM sysobjects sobjects
JOIN sys.dm_db_partition_stats st ON OBJECT_NAME(OBJECT_ID) = sobjects.name
WHERE sobjects.xtype = 'U' and SUBSTRING(sobjects.name,1,10) = 'MyFirstTenCharacter'
 
Share this answer
 
Comments
Member 11125813 6-Dec-14 8:29am    
Thanks but i dont want rowCount , each of 10 tables has an amount fields . i would like to show the sum of amounts of each table in the other col .
Member 11125813 6-Dec-14 8:32am    
And for this : First, you need 1,10in your SUBSTRING, not 0,10 - SQL indexes start at 1.
Both of 0,10 and 1,9 are true .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900