SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


PS script works in ISE but fails in SQL JOB step with a syntax error


PS script works in ISE but fails in SQL JOB step with a syntax error

Author
Message
mstjean
mstjean
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2848 Visits: 2583
I need to copy a subset of rows from a table on a (sql2016) server, to a table on a (sql2008r2) server.
Old approach was SSIS; I wanted to see if I could do it in PS more simply.
I found this article & tried the code submitted by Gungnir (included inline below)

https://community.spiceworks.com/topic/2076861-simpler-way-to-copy-sql-data-between-servers

It works from the PS ISE (on a Windows 10 desktop).

But when I paste it into a SQL JOB step on the SQL2016 box, it fails with a syntax error on this line:
"'$($Row.$Prop)'"

I'd prefer a solution that works for both the ISE & in a SQL JOB step, but if that cannot be done, so be it!
If you suggest troubleshooting steps, assume I am fairly PS-ignorant.


# Here is the code:
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process
Import-Module SQLPS

$SourceServer = 'svrname1'
$SourceDatabase = 'dbname1'
$SourceTable = 'tblname1'
$DestServer = 'svrname2'
$DestDatabase = 'dbname2'
$DestTable = 'tblname2'

$SelectQuery = "SELECT * FROM $SourceTable WHERE (some restriction..not important to discussion)"
$SelectRows = Invoke-Sqlcmd -ServerInstance $SourceServer -Database $SourceDatabase -Query $SelectQuery
$Properties = $SelectRows |
Get-Member -MemberType Property |
Select-Object -ExpandProperty Name

$InsertValues = foreach( $Row in $SelectRows ){
$Values = foreach( $Prop in $Properties ){
"'$($Row.$Prop)'" # Works in PS ISE; But fails with syntax error in SQL JOB STEP *************************************************
}
$Values -join ','
}

$Insert = $InsertValues -join '), ('
$InsertQuery = "INSERT INTO $DestTable ( $( $Properties -join ', ' ) ) VALUES ( $Insert )"
Invoke-Sqlcmd -ServerInstance $DestServer -Database $DestDatabase -Query $InsertQuery




Cursors are useful if you don't know SQL
mstjean
mstjean
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2848 Visits: 2583
Resolved; replaced problem row with this: "'" + $Row.$Prop + "'"

Also needed to modify these rows:
$Insert = $InsertValues -join '), ('
$InsertQuery = "INSERT INTO $DestTable ( $( $Properties -join ', ' ) ) VALUES ( $Insert )"
Invoke-Sqlcmd -ServerInstance $DestServer -Database $DestDatabase -Query $InsertQuery


To this:
$Insert = $InsertValues -join '), ('
$Cols = $Properties -join ', '
$InsertQuery = "INSERT INTO $DestTable ( $Cols ) VALUES ( $Insert )"
Invoke-Sqlcmd -ServerInstance $DestServer -Database $DestDatabase -Query $InsertQuery


This code runs correctly in the ISE & in the SQL JOB.


Cursors are useful if you don't know SQL
Sue_H
Sue_H
SSC Guru
SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)

Group: General Forum Members
Points: 72695 Visits: 14961
mstjean - Thursday, January 4, 2018 4:33 PM
Resolved; replaced problem row with this: "'" + $Row.$Prop + "'"

Also needed to modify these rows:
$Insert = $InsertValues -join '), ('
$InsertQuery = "INSERT INTO $DestTable ( $( $Properties -join ', ' ) ) VALUES ( $Insert )"
Invoke-Sqlcmd -ServerInstance $DestServer -Database $DestDatabase -Query $InsertQuery


To this:
$Insert = $InsertValues -join '), ('
$Cols = $Properties -join ', '
$InsertQuery = "INSERT INTO $DestTable ( $Cols ) VALUES ( $Insert )"
Invoke-Sqlcmd -ServerInstance $DestServer -Database $DestDatabase -Query $InsertQuery


This code runs correctly in the ISE & in the SQL JOB.


Thank you very much for posting back - it really helps everyone out, especially those who come across your post when searching on similar issues.

Sue



Shawn Melton
Shawn Melton
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22226 Visits: 3624
[quote]
mstjean - Thursday, January 4, 2018 1:11 PM
But when I paste it into a SQL JOB step on the SQL2016 box, it fails with a syntax error on this line:
"'$($Row.$Prop)'"
The reason this fails in a PowerShell step of SQL Agent is due to the tokens with Agent jobs. A token, when used in Agent job, is wrapped using "$()", so anything within that is picked up by SQL Agent as a token and it will try to resolve it as one. These are used differently in PowerShell and just don't mix well when you try to use them in a SQL Agent PowerShell job step.

It is best to use a CmdExec step in SQL Agent as it will give you the most consistent experience to running scripts in normal PowerShell or PowerShell ISE.


Shawn Melton
Twitter: @wsmelton
Blog: blog.wsmelton.info
Shawn Melton
Shawn Melton
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22226 Visits: 3624
If your requirements allow for the table to be the same name on your destination as it is on the source, you can utilize the dbatools module to help make maintaining (and in some cases performing) the process much easier. The command that would allow you to easily perform this task is: Copy-Dbatable.


Import-Module dbatools

$SourceServer = 'svrname1'
$SourceDatabase = 'dbname1'
$Table = 'tblname1'
$DestServer = 'srvname2'
$DestDatabase = 'dbname2'
$SelectQuery = "SELECT * FROM $SourceTable WHERE (<some restriction>Wink"

Copy-DbaTableData -SqlInstance $SourceServer -Destination $DestServer -DestinationDatabase $DestDatabase -Table $Table -Query $SelectQuery


Now, if it does not and you still need to keep all the same parameters, the module can still help to simply your code:


# Here is the code:
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process
Import-Module dbatools

$SourceServer = 'svrname1'
$SourceDatabase = 'dbname1'
$SourceTable = 'tblname1'
$DestServer = 'svrname2'
$DestDatabase = 'dbname2'
$DestTable = 'tblname2'

$SelectQuery = "SELECT * FROM $SourceTable WHERE (some restriction..not important to discussion)"
$SelectData = (Connect-DbaInstance -SqlInstance $SourceServer).Databases[$SourceDatabase].Query($SelectQuery)

<# As long as your data types are supported well in BulkInsert, this should work #>
$SelectData | select-object -ExcludeProperty RowError,RowState,Table,ItemArray,HasErrors |
Write-DbaDataTable -SqlInstance $DestServer -Table $DestTable


Shawn Melton
Twitter: @wsmelton
Blog: blog.wsmelton.info
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search