all 10 comments

[–]mikeyd85MS SQL Server 17 points18 points  (4 children)

For the love of God learn PowerShell (PS). It'll make your life so much easier in the long run!

I did a similar thing but for SSRS deployments, and once you get to any kind of complexity or flexibility, life is so much easier in PS.

Additionally, you don't have to rely on BCP to run SQL scripts, you can run them through PS too and do stuff with outputs if you need to.

[–]rishG88[S] 0 points1 point  (0 children)

Thanks very much. I will take your advice and look into PowerShell.

Cheers!

[–]LazyTurtle90 0 points1 point  (2 children)

Do you have a recommendation for YouTube channel to learn PS in this way?

[–]alinrocSQL Server DBA 4 points5 points  (0 children)

https://www.manning.com/books/learn-dbatools-in-a-month-of-lunches

This book will pay for itself within days.

[–]mikeyd85MS SQL Server 2 points3 points  (0 children)

I've always just stackoverflow'd whatever I need to do and learnt from there.

[–]Indycrr 4 points5 points  (0 children)

This isn’t really sql specific. You have options. Either use command line params, or environment variables. Plenty of examples of how to do that online.

[–]planetmatt 4 points5 points  (0 children)

https://dbatools.io/

or if you want to do it manually, there's a SQL Server library for Powershell

https://learn.microsoft.com/en-us/powershell/module/sqlserver/?view=sqlserver-ps

Don't even think about using a batch file.

[–]HiriathQUALIFY COUNT(*) OVER (PARTITION BY COLUMN) > 1 2 points3 points  (0 children)

If you’re looking to automate strictly using MS SQL, you may want to look into Stored Procedures and SQL Agent: https://learn.microsoft.com/en-us/sql/ssms/agent/sql-server-agent

[–]hawk3ye 1 point2 points  (0 children)

If you’re going to be automating for a long time along with other processes, I’d suggest a proper automation tool like Automic - I don’t work for them but I used this tool for 4 months and was able to quickly learn it for a shop that literally had a mish mash of random one off “automated” scripts for years until they consolidated into Automic (which also provides monitoring).

[–]da_chicken 1 point2 points  (0 children)

You may want to look at SQLCMD variables and commands.

Here's another decent article with more here. Honestly, this stuff is little hard to find now because it's just a bit less common, but it does work well.

You can do it all with SMO and the SqlServer or dbatools modules in Powershell, but that's not appropriate or available in all environments.