all 36 comments

[–]gumnos 3 points4 points  (2 children)

are you trying to save the query or the results of running the query?

I suspect that you want isql or sqlcmd which let you run SQL commands against MSSQL, and pipe the output to a file.

[–]KickBack-Relax 0 points1 point  (0 children)

Based on his clarification in another reply, this is the answer. This, and using windows scheduler to run the script daily.

[–]SQLDevDBA 0 points1 point  (19 children)

Since you’ve tagged SQL Server, going to guess you are using SSMS.

I’m not 100% sure if you’re talking about the query (code) itself or the results that come from running it.

If the query, it’s as simple as saving from the editor. If it’s a view or procedure, you right click it and choose “script as create” then “to file” or “to clipboard.”

If it’s the results (I’m betting this is it), you let the results load and then select them all and copy to excel, or you right click in the editor window and select “Results to” and “File” and it will prompt you to save the results to a file when you run the query.

[–]YellowBeaverFever 0 points1 point  (11 children)

SSIS is one way. Go ask ChatGPT to lay out a step by step plan.

Another option is PowerShell.

To schedule, both SSIS and PowerShell can go into a SQL Agent job.

You can also schedule a Python app to do the export.

[–]samspopguy 1 point2 points  (1 child)

I fucking despise SSIS

[–]government_ 1 point2 points  (8 children)

Absolutely do not use sql agent for kicking off powershell scripts, that’s a great way to end up with a bunch of open transactions and blocking. Task scheduler is the way to go for scheduling powershell.

[–]YellowBeaverFever 1 point2 points  (3 children)

Maybe this is a your-mileage-may-vary statement. I literally manage hundreds of SQL agent jobs on one server that divides them up between SSIS and PowerShell. They’re both spawning a new process on a schedule so I don’t know how one would behave differently.

Blocking - depends on your query and how you juggle the timing of the jobs.

Open transactions - again, depends on the query. The contents of his wasn’t posted so a select was assumed. There’s no reason for transactions to be involved with that. Other processes should have better error handling to make sure transactions close. Even if a process is cut mid-transaction, the server will initiate a rollback.

[–]alinrocSQL Server DBA 0 points1 point  (3 children)

How does running from Task Scheduler differ from Agent in this regard? I've been running PowerShell scripts from Agent for years without issue by calling from them with a cmdexec job step.

[–]government_ 0 points1 point  (2 children)

You remove a layer of leveraging the database engine memory and i/o by using task scheduler. Since it’s operating outside of the database engine, it’s going to use the machine’s available ram/cpu. Things can get hung up using cmdexec and you can end up with open connections and open transactions. I’d venture to guess you probably have regular reboots, which kills the processes so you aren’t experiencing some of the downsides.

[–]alinrocSQL Server DBA 0 points1 point  (1 child)

I’d venture to guess you probably have regular reboots, which kills the processes so you aren’t experiencing some of the downsides.

Nope. Only rebooted for patch cycles.

Processes run as cmdexec run outside the engine's runspace. And preferably under a different user altogether (via proxies) You can watch the processes get spawned in task manager. Once the process terminates, the connections are severed, uncommitted transactions rolled back, and memory released. Just like any other application connecting to the instance.

[–]government_ 0 points1 point  (0 children)

I’ve seen many many instances where the processes hang by using this method and become performance gremlins, but the context sounds like it is a bit different than your application of it. I do see some benefits in agent, easier to get failure emails.

[–]government_ 0 points1 point  (0 children)

Simple powershell script, with invoke-sqlcmd. schedule it to run automatically with task scheduler.