PlusMagi's Blog By Pitt Phunsanit Data Export,Powershell Batch Exporter Tables Data in MSSQL Using PowerShell

Batch Exporter Tables Data in MSSQL Using PowerShell

Exporting data from multiple tables in MSSQL can be a tedious task, especially when dealing with a large number of tables. To streamline this process, you can use PowerShell to automate the export of data from specified tables in MSSQL. This article will guide you through creating a PowerShell script to batch export data from multiple tables in MSSQL.

Prerequisites

Before you start, ensure that you have the following prerequisites:

  • PowerShell 5.1 or newer.
  • The SQL Server Management Objects (SMO) component installed on the machine running the script. This is typically included with a full SSMS installation.

Configuration Section

Start by defining the configuration parameters for your script. This includes the SQL Server instance, database names, authentication method, output directory, script options, and the list of tables to export.

PowerShell

# batch_exporter_tables_data.ps1# batch exporter table data# Author: Pitt Phunsanit# --- CONFIGURATION SECTION ---# 1. Define your SQL Server Instance and Database$SqlServerName = "LOCALHOST" # Example: "SQLDEVINSTANCE1" or "LOCALHOST"$DatabaseNameSource = "DB_DEV" # Example: "DB_DEV"$DatabaseNameTarget = "DB_QA" # Example: "DB_QA"# 2. Define the Authentication Method (Choose one of the two blocks below)# --- A) WINDOWS AUTHENTICATION (Recommended) ---#$UseWindowsAuth = $true#$SqlUserName = ""#$SqlPassword = ""# --- B) SQL SERVER AUTHENTICATION (Uncomment and fill in details) ---$UseWindowsAuth = $false$SqlUserName = "pitt" # Example: "sa"$SqlPassword = "phunsanit" # Example: "your_password"# 3. Define the Output Directory$OutputDirectory = "C:portablesSSMSexporter$(Get-Date -Format 'yyyyMMdd_HHmmss')_data" # Change to your desired output path# 4. Script Options$IncludeTruncateStatements = $true # Set to $false if you don't want TRUNCATE TABLE statements$IncludeTruncateComments = $true # Set to $false if you don't want decorative comments around TRUNCATE statements$CommentOutTruncateStatements = $true # Set to $true to wrap TRUNCATE statements in /* */ block comments# 5. List of Tables to Export (Parsed from your request)$TableList = @(    "PP.APPROVE_LOG",    "PP.ORDER"    "PP.PAY_ORDER_EMAIL_LOG",    "PP.PAY_ORDER_PREMIUM_VAT",    "PP.PAY_ORDER_VAT",    "PP.PAY_ORDER",    "PP.PAYMENT_CHEQUE",    "PP.PAYMENT_CYCLE",    "PP.PAYMENT_VOUCHER",    "PP.PERMISSION",    "PP.SEARCH",    "PP.USERS",)

Script Execution

After configuring the script, you can execute it by running the PowerShell script. Ensure that you have the necessary permissions to access the SQL Server instance and the databases.

Conclusion

Using PowerShell to batch export data from multiple tables in MSSQL can significantly streamline the data export process. By automating this task, you can save time and reduce the risk of errors. This script provides a flexible and customizable solution for exporting data from multiple tables in MSSQL.