Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a function that I use (slightly redacted). It allows input and output parameters. I only have uniqueidentifier and varchar types implemented, but any other types are easy to add. If you use parameterized stored procedures (or just parameterized sql...this code is easily adapted to that), this will make your life a lot easier.</p> <p>To call the function, you need a connection to the SQL server (say $conn), </p> <blockquote> <p>$res=exec-storedprocedure -storedProcName 'stp_myProc' -parameters @{Param1="Hello";Param2=50} -outparams @{ID="uniqueidentifier"} $conn</p> <p><em>retrieve proc output from returned object</em></p> <p>$res.data #dataset containing the datatables returned by selects</p> <p>$res.outputparams.ID #output parameter ID (uniqueidentifier)</p> </blockquote> <p>The function:</p> <pre><code>function exec-storedprocedure($storedProcName, [hashtable] $parameters=@{}, [hashtable] $outparams=@{}, $conn,[switch]$help){ function put-outputparameters($cmd, $outparams){ foreach($outp in $outparams.Keys){ $cmd.Parameters.Add("@$outp", (get-paramtype $outparams[$outp])).Direction=[System.Data.ParameterDirection]::Output } } function get-outputparameters($cmd,$outparams){ foreach($p in $cmd.Parameters){ if ($p.Direction -eq [System.Data.ParameterDirection]::Output){ $outparams[$p.ParameterName.Replace("@","")]=$p.Value } } } function get-paramtype($typename,[switch]$help){ switch ($typename){ 'uniqueidentifier' {[System.Data.SqlDbType]::UniqueIdentifier} 'int' {[System.Data.SqlDbType]::Int} 'xml' {[System.Data.SqlDbType]::Xml} 'nvarchar' {[System.Data.SqlDbType]::NVarchar} default {[System.Data.SqlDbType]::Varchar} } } if ($help){ $msg = @" Execute a sql statement. Parameters are allowed. Input parameters should be a dictionary of parameter names and values. Output parameters should be a dictionary of parameter names and types. Return value will usually be a list of datarows. Usage: exec-query sql [inputparameters] [outputparameters] [conn] [-help] "@ Write-Host $msg return } $close=($conn.State -eq [System.Data.ConnectionState]'Closed') if ($close) { $conn.Open() } $cmd=new-object system.Data.SqlClient.SqlCommand($sql,$conn) $cmd.CommandType=[System.Data.CommandType]'StoredProcedure' $cmd.CommandText=$storedProcName foreach($p in $parameters.Keys){ $cmd.Parameters.AddWithValue("@$p",[string]$parameters[$p]).Direction= [System.Data.ParameterDirection]::Input } put-outputparameters $cmd $outparams $ds=New-Object system.Data.DataSet $da=New-Object system.Data.SqlClient.SqlDataAdapter($cmd) [Void]$da.fill($ds) if ($close) { $conn.Close() } get-outputparameters $cmd $outparams return @{data=$ds;outputparams=$outparams} } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload