SQLServerPSModule icon indicating copy to clipboard operation
SQLServerPSModule copied to clipboard

Get-SqlDatabase and Invoke-Sqlcmd Piping

Open van-thieu opened this issue 2 years ago • 2 comments

Is it possible to do the below?

$Database = Get-SqlDatabase -ServerInstance $Server -Name $Name

$Database | Invoke-Sqlcmd -Query "Insert Into..."

van-thieu avatar Nov 08 '23 20:11 van-thieu

Hi @van-thieu, that is not supported (at least today).

Have you considered something like:

$Database = Get-SqlDatabase -ServerInstance $Server -Name $Name
$Database | % { Push-Location (Convert-UrnToPath $_.Urn); Invoke-Sqlcmd -Query  "Insert Into..." -SuppressProviderContextWarning; Pop-Location }

or

$Database = Get-SqlDatabase -ServerInstance $Server -Name $Name
$Database.Parent | Invoke-Sqlcmd -Query '...' -Database $Database.Name 

I suppose I could make the cmdlet a little bit more tolerant and accept a SMO Database object (the type returned by Get-SqlDatabase) and not just SMO Server or string.

The "problem" is that today we are stuck with -ServerInstance being the argument from the pipeline... and changing that would be a breaking change for many people.

Perhaps, we could just keep the -ServerInstance name as it is and accept the database object as well. or something like that... it would be a bit (or a lot) confusing though...

Matteo-T avatar Nov 16 '23 02:11 Matteo-T

Thanks. Hopefully, it could be more dynamic in the future.

van-thieu avatar Nov 16 '23 02:11 van-thieu