How to run a stored procedure every day in SQL Server Express Edition?

前端 未结 11 1496
野的像风
野的像风 2020-11-28 08:06

How is it possible to run a stored procedure at a particular time every day in SQL Server Express Edition?

Notes:

  • This is needed to truncate an audit t
11条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 08:56

    Create a scheduled task that calls "C:\YourDirNameHere\TaskScript.vbs" on startup. VBScript should perform repeated task execution (in this example, it's a 15 minute loop)

    Via command line (must run cmd.exe as administrator):

    schtasks.exe /create /tn "TaskNameHere" /tr "\"C:\YourDirNameHere\TaskScript.vbs\" " /sc ONSTARTUP
    

    Example TaskScript.vbs: This executes your custom SQL script silently using RunSQLScript.bat

    Do While 1
        WScript.Sleep(60000*15)
        Set WshShell = CreateObject("WScript.Shell")
        WshShell.RUN "cmd /c C:\YourDirNameHere\RunSQLScript.bat C:\YourDirNameHere\Some_TSQL_Script.sql", 0
    Loop
    

    RunSQLScript.bat: This uses sqlcmd to call the database instance and execute the SQL script

    @echo off
    sqlcmd -S .\SQLEXPRESS -i %1
    

提交回复
热议问题