Posts

Showing posts from December, 2011

How to find a table when you don't know which database it belongs to?

In this very short tip I'd like to share my solution to a yesterday quesiton in MSDN forum - how to find the database the table belongs to. I use my favorite idea of dynamically constructing a query using INFORMATION_SCHEMA.TABLES view. DECLARE @table sysname, @SQL NVARCHAR(MAX) SET @table = 'Items' SET @SQL = '' SELECT @SQL = @SQL + '; IF EXISTS (SELECT 1 from ' + QUOTENAME(name) + '.INFORMATION_SCHEMA.Tables WHERE Table_Name = @table AND TABLE_TYPE = ''BASE TABLE'') PRINT ''Table ' + @table + ' found in ' + name + '''' FROM sys.databases   EXECUTE sp_executeSQL @SQL, N'@table sysname', @table That's all. Try it with the table which you may have in multiple databases. I used a bit undocumented way of concatenating multiple rows in a string, so I can not actually use ORDER BY in this code. To be strict, I need to use XML PATH('') approach here instead.

15 GREAT THOUGHTS BY CHANAKYA

Image
1) "Learn from the mistakes of others... you can 't live long enough to make them all yourselves!!" - Chanakya 2)"A person should not be too honest. Straight trees are cut first and Honest people are screwed first." - Chanakya 3)"Even if a snake is not poisonous, it should pretend to be venomous." Chanakya 4)"There is some self-interest behind every friendship. There is no friendship without self-interests. This is a bitter truth." - Chanakya                                                              5)" Before you start some work, always ask yourself three questions - Why am I doing it, What the results might be and Will I be successful. Only when you think deeply and find satisfactory answers to these questions, go ahead." - Chanakya 6)"As soon as the fear approaches near, attack and destroy it." - Chanakya 7)"The world's biggest power is the youth and beauty of a woman." - Chanakya