*******EDITED*****
I have multiple sql servers with around 200-300 dbs in them and want to save space on my server.
I would like to run a shrink routine on all databases in my sql server by running this script through a task scheduler. I have the queries but I do not know how to connect the two together.
For selecting all databases I use this
select * from sys.sysdatabases
Where name <> 'master' and name <> 'tempdb' and name <> 'model' and name <> 'msdb'
For my shrink routeen I use this
USE [single_database_name]
GO
DBCC SHRINKFILE ('single_database_name', 10)
GO
How can I connect the two queries so that the “single_database_name” is coming from the list of all database names from the first query.
Thanks for your help
Use a cursor with dynamic SQL. This will shrink each file individually on each database.