Share via

I have SQL querry that I want to run on a scheudle ever other day

Gary Henry 21 Reputation points
2025-09-29T21:23:17.39+00:00

Hi

Not sure if this is the right place to ask so if not I hope someone can direct me...

I have a simple sql query that was written for me (see below) that I now need to run on a schedule and export the data to a .CSV file with Headers and save it so I can use it in another process. It looks like I can do this in poweshell but I do not know how to take the below script and either reference the script itself or put the script into powershell.

I only have ReadOnly access to the SQL database.

I can run this from the management console, but I have the need now to automate the process

USE lc_V3

SELECT

   Items.iID AS product_id,

   LTRIM(RTRIM(s.ProductNumber)) AS mpn

FROM Items

LEFT JOIN POS..Skus s ON Items.strSku = s.Sku

WHERE ISNULL(LTRIM(RTRIM(s.ProductNumber)), '') <> '';

Any help guidance or direction would be welcome

Gary

SQL Server Database Engine
0 comments No comments

Answer accepted by question author

Marcin Policht 90,150 Reputation points MVP Volunteer Moderator
2025-09-29T21:31:33.64+00:00

Step 1: Create the PowerShell script (Export-Query.ps1)

# Parameters
$SqlServer = "YOUR_SQL_SERVER_NAME"
$Database = "lc_V3"
$OutputFile = "C:\Exports\Products.csv"

# SQL Query
$Query = @"
SELECT
   Items.iID AS product_id,
   LTRIM(RTRIM(s.ProductNumber)) AS mpn
FROM Items
LEFT JOIN POS..Skus s ON Items.strSku = s.Sku
WHERE ISNULL(LTRIM(RTRIM(s.ProductNumber)), '') <> '';
"@

# Load SQL client
$connectionString = "Server=$SqlServer;Database=$Database;Integrated Security=True;"
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString

$command = $connection.CreateCommand()
$command.CommandText = $Query

$adapter = New-Object System.Data.SqlClient.SqlDataAdapter $command
$table = New-Object System.Data.DataTable

# Fill table with results
$adapter.Fill($table) | Out-Null

# Export to CSV with headers
$table | Export-Csv -Path $OutputFile -NoTypeInformation -Encoding UTF8

Write-Host "Export completed: $OutputFile"

Step 2: Run the script

  • Save the script as Export-Query.ps1.
  • Run it manually first:
      powershell.exe -ExecutionPolicy Bypass -File "C:\Path\Export-Query.ps1"
    

Step 3: Automate with Task Scheduler

  1. Open Task Scheduler.
  2. Create a new task → Trigger it (daily, hourly, etc.).
  3. Action → Start a Program → Program: powershell.exe Arguments:
       -ExecutionPolicy Bypass -File "C:\Path\Export-Query.ps1"
    

If your SQL login uses SQL authentication instead of Windows Auth, change the connection string:

  $connectionString = "Server=$SqlServer;Database=$Database;User Id=USERNAME;Password=PASSWORD;"

Make sure the folder C:\Exports\ exists (or update $OutputFile path).


If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

hth

Marcin

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.