microsoft / SQLServerPSModule

This repo is the home of SQL Server PowerShell Module development.
MIT License
45 stars 1 forks source link

Get-SqlDatabase and Invoke-Sqlcmd Piping #63

Open van-thieu opened 7 months ago

van-thieu commented 7 months ago

Is it possible to do the below?

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

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

Matteo-T commented 7 months ago

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...

van-thieu commented 7 months ago

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