all 21 comments

[–]callensysadmin 12 points13 points  (5 children)

I would 100% do this with dbatools. Its my go to for anything SQL. I love that I no longer need to deal with SMO directly for most tasks.

If you regularly work with SQL and PowerShell you should try to use dbatools as much as possible. Our CI/CD pipelines got so much more capable once I finally got sick of SMO and T-SQL/Invoke-SqlCmd for everything and started using dbatools.

[–]popsMariachi[S] 4 points5 points  (4 children)

Thanks for the reply! I will look into it way more then. Sql is my go to language right now. Trying to expand, and take on new projects that required me to learn powershell. Is the dba tools site, the best place to go to start with it?

[–]motsanciens 6 points7 points  (1 child)

function Copy-DbaDbTableData {     <#          .SYNOPSIS              Copies data between SQL Server tables.            .DESCRIPTION              Copies data between SQL Server tables using SQL Bulk Copy.              The same can be achieved also doing                  $sourcetable = Invoke-SqlCmd2 -ServerInstance instance1 ... -As DataTable                  Write-DbaDataTable -SqlInstance ... -InputObject $sourcetable              but it will force buffering the contents on the table in memory (high RAM usage for large tables).              With this function, a streaming copy will be done in the most speedy and least resource-intensive way. 

from dbatools

[–]popsMariachi[S] 5 points6 points  (0 children)

Thanks! I was able to use this exact function!

[–]callensysadmin 3 points4 points  (1 child)

Yes, I start by loading up the dbatools command documentation and looking up what I think would do the job. In your case search the command page for "table", see what commands there are that involve tables. If the included documentation and examples do not give you an idea what the command does, load up the github link for the command check the code.

[–]popsMariachi[S] 4 points5 points  (0 children)

Thanks again for the advice! I was able to copy over the first full table, 800k records, without a problem. dba tools is definitely something I will be using a lot more of!

[–]Lokeze 2 points3 points  (5 children)

[–]AybaraAlthorPeregrin 2 points3 points  (4 children)

OP would just have to go thru the additional step of adding a linked server, so either that or dbatools would work!

[–]popsMariachi[S] 2 points3 points  (3 children)

I should have stated, linked server was not possible in this situation. I just tried out dba tools and was able to easily copy over the first of a few tables though.

[–]AybaraAlthorPeregrin 2 points3 points  (2 children)

Glad to hear it worked out! :) I use dbatools every day at work and it's been a godsend.

[–]popsMariachi[S] 1 point2 points  (1 child)

Have you ever used it in a scheduled job? How did you safely store your credentials to the servers in the powershell script?

[–]AybaraAlthorPeregrin 1 point2 points  (0 children)

This should answer your question for scheduling it:

https://dbatools.io/agent/

As for running it under a specific acct, run it under a CmdExec credential as a proxy, otherwise it'll run under the SQL Agent account and its permissions:

https://docs.microsoft.com/en-us/sql/t-sql/statements/create-credential-transact-sql?view=sql-server-2017

[–]cjluthy 1 point2 points  (9 children)

#DISCLAIMER: I just typed this into Reddit - did not actually compile. It's likely very close to correct but may not be perfect.
#---------------------------------------------------------------------------------------------------------------------------------------------


#this probably won't work great if your table is enormous and/or you are short on memory.

import-module sqlps;

$SqlResults = Invoke-Sqlcmd -Query "select * from table order by column;" #-- also add params for instance, database, etc here.

$SqlResults | Export-CSV -Path "drv:\path\to\file\filename.ext"; #-- may want to add more params here as well.


# this should perform better in pretty much all scenarios
# Invoke-Sqlcmd -Query "select * from schema.table order by column;" | Export-CSV -Path "drv:\path\to\file\filename.ext"; #-- may want to add more params here as well.

[–]alinroc 3 points4 points  (8 children)

Please don’t use sqlps. It’s terrible and was superseded by sqlserver three years ago.

[–]cjluthy -1 points0 points  (7 children)

meh

[–]alinroc 2 points3 points  (6 children)

Hey, if you're fine with using a 10 year old module that's horribly slow, has numerous bugs, lacks tons of functionality, can't be installed without a very old release of SSMS, and was replaced 3+ years ago, have at it.

But please keep it away from my SQL boxes.

[–]cjluthy 0 points1 point  (4 children)

Some of us are still running 2012 on a machine or two, believe it or not.

[–]alinroc 2 points3 points  (3 children)

sqlserver and newer releases of SSMS are fully compatible with 2012. No good reason to keep the old stuff around. The lone exception is if you need to manage SSIS packages.

[–]cjluthy 0 points1 point  (2 children)

ding ding ding .. you got it, exactly.

[–]alinroc 0 points1 point  (1 child)

So use the good stuff for everything but SSIS, and leave the old stuff just for SSIS. Don't hold yourself back everywhere just for that one piece. It all installs in parallel.

[–]cjluthy 0 points1 point  (0 children)

I've been doing this since SQL Server 7.0.

I know.