If you are interested in keeping a local copy of your data here is practical way to complete this. In order for this to work you will need the Advanced Data Access feature in order for this to be an option
Add SQLCMD To the PC that you would like to use:
Option 1: Install SQLCMD via Microsoft ODBC Driver for SQL Server
This is the recommended approach if you only need sqlcmd
and not the full suite of SQL Server tools.
Download the Microsoft ODBC Driver for SQL Server:
- Visit the official Microsoft download page for ODBC Driver for SQL Server.
- Download the latest version of the ODBC driver compatible with your system (Windows 11 should support most recent versions).
Download the Command Line Utilities:
- On the same page, look for the "Command Line Utilities" download link. This package includes
sqlcmd
andbcp
. - Download and run the installer.
- On the same page, look for the "Command Line Utilities" download link. This package includes
Install the Utilities:
- Follow the on-screen instructions to install the utilities.
- By default,
sqlcmd
will be added to your system's PATH, so you can use it directly from the command prompt or PowerShell.
Option 2: Install SQL Server Management Studio (SSMS)
If you need a graphical interface for SQL Server management along with sqlcmd
, installing SSMS might be a better option.
Download SQL Server Management Studio:
- Visit the official Microsoft download page for SQL Server Management Studio (SSMS).
- Download the latest version of SSMS.
Install SSMS:
- Run the installer and follow the on-screen instructions.
sqlcmd
is installed automatically as part of SSMS, and it will be added to your system's PATH.
Create a backup.bat file. Add the following to it:
@echo off
REM Define variables for database connection
set server_name=tcp:myserver.database.windows.net,1433
set database_name=evo
set username=myUsername
set password=myPassword
REM Define where the output should be stored.
set output_folder=C:\Exports
REM Delete all existing .sql files in the output folder
del %output_folder%\*.sql /Q
REM Read tables from the configuration file
for /F "tokens=*" %%t in (backuptables.conf) do (
sqlcmd -S %server_name% -d %database_name% -U %username% -P %password% -Q "SELECT * FROM schema_name.%%t" -o "%output_folder%\%%t.sql"
echo Exported %%t to %output_folder%\%%t.sql
)
echo Data export completed.
pause
Step-by-Step Guide to Schedule a Script in Windows Task Scheduler
1. Open Task Scheduler
- Access Task Scheduler:
- Press
Win + S
to open the search bar. - Type Task Scheduler and press Enter to open the Task Scheduler application.
- Press
2. Create a New Task
Create a Basic Task:
- In the Task Scheduler window, click on Create Basic Task on the right side under the Actions pane.
Name Your Task:
- Give your task a meaningful name (e.g.,
Export SQL Data
). - Optionally, add a description for clarity (e.g.,
This task exports data from SQL tables to .sql files
). - Click Next.
- Give your task a meaningful name (e.g.,
Choose When to Start the Task:
- Choose how often you want the task to run: Weekly, Monthly, etc.
- Click Next and specify the exact time and frequency according to your needs.
- Click Next.
Set the Action to Start a Program:
- Choose Start a program as the action to perform.
- Click Next.
3. Configure the Task to Run Your Script
Select the Script or Program to Run:
- In the Program/scriptfield:
- For a batch script (
.bat
), browse and select your backup.bat
file.
- For a batch script (
- In the Program/scriptfield:
Start In (Optional):
- Optionally, set the Start in field to the directory where your script resides. This can be helpful if your script depends on relative paths.
Finish the Task Setup:
- Click Next to review your task details.
- Click Finish to create the task.
4. Adjust Advanced Settings (Optional)
Open Task Properties:
- Find your newly created task in the Task Scheduler Library.
- Right-click the task and select Properties.
General Tab:
- Check Run whether user is logged on or not if you want the task to run without requiring user logon.
Triggers Tab:
- You can add additional triggers here, such as running the task on system startup or when a specific event occurs.
Conditions Tab:
- Adjust conditions like only starting the task if the computer is idle, or if it's on AC power (useful for laptops).
Settings Tab:
- Set options like allowing the task to be run on demand, stopping the task if it runs longer than expected, etc.
Security Options:
- Make sure to choose Run with highest privileges if your script requires administrative rights.
Save Changes:
- Click OK to save your settings.
5. Test the Task
- Run the Task Manually:
- In Task Scheduler, right-click on your task and select Run to test it immediately.
- Check your output folder to verify that the script ran successfully and generated the expected files.
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article