|
Hi, 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.
(comments are locked)
|
|
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: Regards Dirk
(comments are locked)
|
|
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. Nice one.
Sep 28 '12 at 08:31 AM
DirkHondong
(comments are locked)
|



Just a warning - sp_msforeachDB is buggy and has a habit of missing databases. You would be better off dumping a list of database into a table variable and iterating through that to be sure you aren't missing anything.
Good point. Maybe take a look here: http://sqlblog.com/blogs/aaron_bertrand/archive/2010/12/29/a-more-reliable-and-more-flexible-sp-msforeachdb.aspx Offers an alternative