powershell - Concat invoke-SqlCmd query string does not work -
how can concat list of parameter or string of parameter (better) sql query? below not work.
$parameters = @("-serverinstance `"mymachine\sqlexpress`"", "-database %targetdbname%", "-username %sqlusername%", "-password %sqlpassword%") $row = invoke-sqlcmd -query "select field table;" $parameters
i want execute later multiple queries same connection parameters , usefull reuse them in string can add query string.
you on right track. sounds looking splatting.
splatting method of passing collection of parameter values command unit.
i don't use invoke-sqlcmd should work this:
$parameters = @{ serverinstance = "mymachine\sqlexpress" database = "targetdbname" username = "sqlusername" password = "sqlpassword" query = "select field table;" } $row = invoke-sqlcmd @parameters
collect parameters hashtable , splat cmdlet. if wanted use parameter set again later, make small changes, easy referencing name/value pair of hashtable.
$parameters.query = "select field differenttable;" $anotherrow = invoke-sqlcmd @parameters
Comments
Post a Comment