Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9100361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:50:43+00:00 2026-06-17T00:50:43+00:00

** * **** EDITED * ** * * I have multiple sql servers with

  • 0

*******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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T00:50:45+00:00Added an answer on June 17, 2026 at 12:50 am

    Use a cursor with dynamic SQL. This will shrink each file individually on each database.

    Declare @dataFiles Table (databaseName Varchar(256), datafile Varchar(256))
    Declare @SQL Nvarchar(Max), @databaseName Varchar(256), @dbfile Varchar(256)
    
    Insert  @dataFiles
    select  sd.name, smf.name
    from    sys.sysdatabases sd
    join    sys.master_files smf
            On  sd.[dbid] = smf.database_id
    Where   sd.name not in ('master','tempdb','model','msdb')
    
    Declare cur Cursor For      
    Select  databaseName,
            datafile
    From    @dataFiles
    
    Open    cur
    Fetch   Next 
    From    cur
    Into    @databaseName,
            @dbfile
    
    While   @@Fetch_Status = 0
    Begin
            Set     @SQL = 'USE [' + @databasename + ']
                            DBCC SHRINKFILE (''' + @dbfile + ''', 10) WITH NO_INFOMSGS'
    
            Exec    sp_executeSQL @SQL
    
            Fetch   Next 
            From    cur
            Into    @databaseName,
                    @dbfile
    End
    Close   cur
    Deallocate cur
    
    select  sd.name As DatabaseName, smf.name DBFileName, (size*8)/1024 SizeMB
    from    sys.sysdatabases sd
    join    sys.master_files smf
            On  sd.[dbid] = smf.database_id
    Where   sd.name not in ('master','tempdb','model','msdb')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have two joined tables in a SQL Server database, one with news
I have an editor view that can be used for multiple edited objects. The
I have a large .aspx page, with multiple server controls. And, there is also
I have a batch file which runs multiple sql files and passes parameters to
I want to convert Following sql server query with Linq-to-sql query . What I
I have a spreadsheet that is edited by multiple users. To prevent tampering with
EDIT: I have edited my post... Working on a project (c#), I have a
i have an edited version of a config file specific for my machine. i
I have an XML EDITED: <?xml version=1.0 encoding=UTF-8?> <?xml-stylesheet type=text/xsl href=xsl.xslt?> <CATALOG> <CD> </CD>
--edited for clarity (hopefully) I have an XML file that looks something like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.