.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> SQL Query >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

How to list all tables in all databases

Posted By:Kemal AL GAZZAH       Posted Date: June 25, 2012    Points: 40    Category: SQL Query    URL: http://www.dotnetspark.com  

This sql script allows you to make a list of all tables in all databases The result is stored in a tables called tab_alldatabases, and you can query this table to find a particular table and locate it in its corresponding data bases
 

--usp_alldatabases 'employe'

create proc usp_alldatabases(@table as nvarchar(1000))
as
begin
--Script to make a list of all tables
--in all datatabses
--and store them in tab_alldatabases
-- By KAMEL AL GAZZAH
--kamelgazzah@gmail.com

declare @script as nvarchar(2000)
if  exists(select 1 from sysobjects where name='tab_alltables') drop table tab_alltables
create table tab_alltables (db nvarchar(1000), tab nvarchar(1000),cdate datetime)
declare c cursor  for
select 'insert into tab_alltables (tab,db,cdate) select name,'''+name+ ''',crdate from ' +name+'..sysobjects where xtype=''u''' from master..sysdatabases  where dbid>4
open c
fetch c into @script
while @@fetch_status=0
begin
exec (@script)

print @script
fetch c into @script
end
close c deallocate c
select * from tab_alltables where tab=@table
end

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend