I am using the following query using sp_MSforeachdb to get the table count of All the tables in specific databases, say about db1, db2, db3 & db4.
This query working fine, But, On the result set I would like to include a column for the Database name.
Including the ''?'' as one of the column is providing some errors and throwing errors on the other databases on the instance except db1, db2, db3 & db4.
Help to include the Database column is very much appreciated and thanks in advance.
------ Script to get all User table counts on specific databases ------ Requirement : Need to include the Database Name on the result set.
Just a quick and dirty solution, but it may fit your needs. I've added a column in the temp table and just using dbname()
the result will look like this:
I like Dirk's answer (+1 to him), but I think this may be more what you were expecting:
The tricky part is you need 4 single quotes on either side of the ? in the select statement because it is already inside of two layers of nested quotes (one starts before the if, the other starts after Exec).
Something like this:
works with just two becuase it is only inse of one layer of quotes at that point.
answered Sep 27 '12 at 07:54 PM