query to check every table

How can you convert the following code, so that it checks for all tables, and not just the one mentioned?

SELECT * FROM information_schema.routines ISR WHERE charindex('TableName', ISR.ROUTINE_DEFINITION)>0
LVL 6
anushahannaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chapmandewCommented:
this should do it:

SELECT * FROM
information_schema.tables t join information_schema.routines ISR on ISR.ROUTINE_DEFINITION LIKE '%' + t.Table_Name + '%'
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alainbrydenCommented:
You need to loop over all tables? The code for this isn't too complicated. It's a little tough to come up with on your own the first time, but after that you can recycle it for all sorts of purposes.

Attached is the code that I use to loop over all databases (and shrink them). All you need to do is change up the contents of the "Execute" command to contain anything you want to apply to all databases.

--
Alain
ALTER PROCEDURE [dbo].[Shrink All Databases]
AS
BEGIN
	DECLARE @DBName nvarchar(max)
	DECLARE @DBs CURSOR
	PRINT('Shrinking all databases... ')
	SET @DBs = CURSOR FOR
		(SELECT name FROM [Master].[sys].[databases])
	OPEN @DBs
	FETCH NEXT FROM @DBs INTO @DBName
	WHILE @@FETCH_STATUS = 0
	BEGIN
		PRINT('Attempting to Shrink database ' + @DBName)
		Execute('Use [' + @DBName + ']
		DBCC SHRINKFILE (1, TRUNCATEONLY) WITH NO_INFOMSGS
		DBCC SHRINKFILE (2, EMPTYFILE) WITH NO_INFOMSGS
		')
		FETCH NEXT FROM @DBs INTO @DBName
	END
	CLOSE @DBs
	DEALLOCATE @DBs
END

Open in new window

0
alainbrydenCommented:
lmao, here we are talking about tables and I've given you my code for looping over all DATABASES.
Looks like I shouldn't have skipped my coffee this morning ;)

--
Alain
0
anushahannaAuthor Commented:
Thanks, Tim.

No, problem, Alain. out of curiosity, what are you doing with 'DBCC SHRINKFILE (2, EMPTYFILE)': where does the log file get recreated with, and what is the benefit of doing this?

0
alainbrydenCommented:
I use that script on a data source that's getting short on space. I don't really care about logging changes in my databases, they're all backed up elsewhere, so running this truncates the transaction files to nothingness (saving a ton of space on databases that get a lot of activity) and also removes all unused but allocated space in databases (useful when tables get millions of rows written to them, and then get cleared out. The space isn't actually freed until shrinkfile is run)

--
Alain
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.