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 8424323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:57:27+00:00 2026-06-10T03:57:27+00:00

I have a bunch of TSQL scripts that need to be executed on a

  • 0

I have a bunch of TSQL scripts that need to be executed on a daily basis.
I know I can use Job Agent to execute them but that requires me to change the actual job.

What I would like to do is create a job that simply says:

execute all TSQL-scripts in <some folder>

A bonus would be if a filter could be used based on the filename of the script: So that one job would execute all the files whose name start with a ‘d’, another job would execute all those with a ‘w’ in the name.

Can this be done? How can this be done?

I read some things that spoke of using the Windows-scheduler to run the SQLCMD-utility.
I’d rather have the SQL Server do the scheduling and executing. Is Powershell the way to go? If so, where and how to start? (Never had to use it so never gave it much attention :/)

Thanks for thinking with me!

Henro

  • 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-10T03:57:28+00:00Added an answer on June 10, 2026 at 3:57 am

    João, thanks for your help. Using your code I produced this: set dateformat dmy

    DECLARE @scripts varchar(100) = 'C:\MYSCRIPTS\'     -- folder with scripts
    DECLARE @project varchar(100) = 'PROJECTX'  -- specific folder
    DECLARE @Identifier varchar(1) = 'D' -- All files of which the name starts with a 'T'
    DECLARE @files  varchar(100) = @scripts + @project + '\' + @Identifier + '*.sql'
    
    CREATE TABLE #tbl1 (Name varchar(400))
    DECLARE @cmd varchar(100) = 'dir ' + @files 
    INSERT #tbl1 EXECUTE master.dbo.xp_cmdshell @command_string = @cmd
    DELETE FROM #tbl1 WHERE  ISDATE(SUBSTRING(Name,1,10))  = 0
    UPDATE #tbl1 SET Name = SUBSTRING(Name,37, 100)
    
    CREATE TABLE #tbl2 (Counter smallint Primary Key IDENTITY(1,1), Name varchar(400))
    INSERT INTO   #tbl2 (Name)
    Select  @scripts + @project + '\' + Name from #tbl1
    
    DECLARE @i int
    DECLARE @NumRows int
    DECLARE @File2BExecuted varchar(100)
    
    SET @i = 1
    SET @NumRows = (SELECT COUNT(*) FROM #tbl2)
    IF @NumRows > 0
        WHILE  (@i <= (SELECT MAX(Counter) FROM #tbl2))
        BEGIN
        SELECT @File2BExecuted = Name FROM #tbl2 WHERE Counter = @i
        DECLARE @script varchar(100) = 'sqlcmd -S ' + @@SERVERNAME + ' -i ' + @File2BExecuted
        EXECUTE master.dbo.xp_cmdshell @command_string = @script
    
        SET @i = @i + 1
        END
    
    drop table #tbl1
    drop table #tbl2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have bunch of objects that return a DOM-tree that I then use to
I have bunch of action-methods that need to verify the ownership of the orderId
I have a bunch of html files that come with my app and are
I have a bunch of code in a routine that looks a bit like
I have a bunch of strings that are dependent on static dictionaries and each
I have a bunch of buttons that have a tapGestureRecognizer linked to them, and
I have bunch of ruby scripts in a git repository and it seems to
I have bunch of html and I need to get all the anchors and
I have bunch of methods in which i need to test whether remote server
I have a bunch of TTable and TSQL Components displayed on a datamodule. If

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.