SQL Server table data count in Excel?

ansarij

New Member
Joined
Dec 29, 2014
Messages
1
Hello All, I am new to SQL server and VBA, would like to import the table rows count to my excel spread sheet. I have 2000 tables in MS SQL Server.
My objective is to get the table name from column A of spreadsheet, execute count(*) query for table and return the value to next column in spreadsheet.

Thanks
Ansarij
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi and welcome to the forum.

This is quite a difficult task for someone who is new to both VBA and SQL Server. I'll give you some key words to research:

You can use a For Each... Next loop to iterate through the cells containing the table names. In the loop you can use each table name to dynamically build a T-SQL query string and then use ADO to connect to your SQL server database to execute the query and return the row count.

That said, I don't particularly like the COUNT(*) approach you have suggested. If you just want a row count of all tables in your database then you can write much more efficient queries than using COUNT(*) for every table. Table names and row counts are housed in sys.objects and sys.partitions. Google is your friend. Use SSMS to write and test your query first before trying to replicate it in Excel.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,217,416
Messages
6,136,508
Members
450,017
Latest member
Alvi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top