Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
DarthRoblox
Nov 25, 2007
*rolls ankle* *gains 15lbs* *apologizes to TFLC* *rolls ankle*...
Hi!

I'm completely new to Powershell, but I'm interesting in learning and I have a task that I'd like to automate. Basically, I need to run a series of SQL queries and then export all columns except the leftmost of each result to it's own Excel file with a slightly different set of headings. I'd then need it to save all of the files in a new folder in a directory, with the files titled like '[Date]_[a]_Import.xlsx'.

The structure would look like:
run SQL a, export to a.xlsx in xxx\[Date]_Imports
run SQL b, export to b.xlsx in xxx\[Date]_Imports
run SQL c, export to c.xlsx in xxx\[Date]_Imports

I can change the queries if removing the left column or reformatting the headings is too much, but I'm curious if this is doable with Powershell. Anyone feel like lending a hand?

Adbot
ADBOT LOVES YOU

DarthRoblox
Nov 25, 2007
*rolls ankle* *gains 15lbs* *apologizes to TFLC* *rolls ankle*...
Sure, just checked and CSVs import fine.

Thanks!

DarthRoblox
Nov 25, 2007
*rolls ankle* *gains 15lbs* *apologizes to TFLC* *rolls ankle*...

adaz posted:

Each SQL query is unique? How many are there? Is there any reason why you need to exclude the leftmost column as opposed to just not querying for that data?

To get started what I'd use is a function I wrote long ago and my normal method of avoiding export-CSV's... eccentricities. Yes it would be a glorious world where we could just pipe the DataSet to export-csv and it'd handle it gracefully... unfortunately that world doesn't exist.

Well, the main reason I was hoping to exclude the leftmost column is because the (lengthy) query was written by someone else who didn't comment anything. However, after actually looking at it I found out that it's fairly simple and removing the column wasn't a problem.

The only difference between the queries is a "where" clause that's in a few different locations (select a where name='x', select a where name='y', select a where name='z')

Since the actual difference between queries is small, how hard would it be to make powershell loop through and replace each instance of x then y then z while still exporting each set of results to a csv?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply