Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
in my store procedure i want to get list of all the tables of a specific database when i give database name as input.for example when i give sp_list(master) it shows me the tables of master database.,how can do this using msforeachtable?
i found this code

SQL
sp_msforeachtable ' select ''?'' as ''Table'', count(*) as ''Rows'' from ? '


but cannot give database name in it as input
Posted

1 solution

SQL
declare @query as nvarchar(max)
declare @dbname as nvarchar(max)
SET @query=''
SET @dbname ='TST'
SET @query =@query + @dbname + '..sp_msforeachtable '' select ''''?'''' as ''''Table'''', count(*) as ''''Rows'''' from ? '''

EXEC sp_executesql @query


Hope this helps if yes then accept the answer and vote it otherwise revert back with your queries
--Rahul D.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900