1

I have a Powershell script that is successfully connecting to a SQL Server database and executing a procedure. The SQL procedure contains a select as follows:

SELECT @sql AS 'ColDemo1'

The variable @sql is nvarchar(max). I want the full contents of @sql returned to a new sql file however I only get the first line and an unwanted column heading as below:

ColDemo1
------------                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
First line of data...                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    

The Powershell script is as follows:

$server = "SERVER\DEMO"
$database = "dbDemo"
$pZero = "PVal0"
$pOne = "PVal1"
$pTwo = "PVal2"
$pThree = "PVal3"

function Run-SQLUSP {
    param (
        $pZero,
        $pOne,
        $pTwo,
        $pThree
    )

    $conn = New-Object System.Data.SqlClient.SqlConnection("Server=${server};Database='${database}';Integrated Security=TRUE")
    $conn.Open()
    $cmd = $conn.CreateCommand()
    $cmd.CommandText = "dbo.demoSp '$pZero', '$pOne', '$pTwo', '$pThree'"
    $adapter = New-Object System.Data.SqlClient.SqlDataAdapter($cmd)
    $dataset = New-Object System.Data.DataSet
    [void]$adapter.Fill($dataset)
    $dataset.tables[0]
}

Run-SQLUSP $pZero $pOne $pTwo $pThree | Out-File "c:\DemoFolder\DemoScriptName.sql" -width 8000

How can I amend my Powershell script to return all lines of the output to the new sql file? When executing the procedure in SSMS the resultset is returned in a single table cell (not multiple rows).

3
  • try using export-csv instead of out-file Commented Sep 14, 2017 at 17:26
  • @brendan62269 thanks, using export-csv avoids the truncation issue. The result however still includes the column heading in quotes and the data is in quotes also. How can I just return the first cell value? note. as well as changing Out-File I also removed -width 8000 and added -NoTypeInformation. Commented Sep 15, 2017 at 6:38
  • I've tried appending .rows[0][0] to $dataset.tables[0] within the function, however this returns Length in quotes and a number which I assume is the string length, also in quotes. Can I use something similar to just return the first cell value? Commented Sep 15, 2017 at 7:20

2 Answers 2

1

Two changes were required:

  • To avoid truncating the output: Change Out-File to Set-Content -Path and remove -width 8000.
  • To only return the first cell: Append .rows[0][0] to $dataset.tables[0] within the function.
  • Below is the revised code in full:

    $server = "SERVER\DEMO"
    $database = "dbDemo"
    $pZero = "PVal0"
    $pOne = "PVal1"
    $pTwo = "PVal2"
    $pThree = "PVal3"
    
    function Run-SQLUSP {
        param (
            $pZero,
            $pOne,
            $pTwo,
            $pThree
        )
    
        $conn = New-Object System.Data.SqlClient.SqlConnection("Server=${server};Database='${database}';Integrated Security=TRUE")
        $conn.Open()
        $cmd = $conn.CreateCommand()
        $cmd.CommandText = "dbo.demoSp '$pZero', '$pOne', '$pTwo', '$pThree'"
        $adapter = New-Object System.Data.SqlClient.SqlDataAdapter($cmd)
        $dataset = New-Object System.Data.DataSet
        [void]$adapter.Fill($dataset)
        $dataset.tables[0].rows[0][0]
    }
    
    Run-SQLUSP $pZero $pOne $pTwo $pThree | Set-Content -Path "c:\DemoFolder\DemoScriptName.sql"
    
    Sign up to request clarification or add additional context in comments.

    Comments

    0

    I was having the same issue, then discovered I could fix it if I used the Column Name to access the cell value from the table row.

    #Trunacted
    $data.Tables[0].Rows[0][0]
    
    #Complete data
    $data.Tables[0].Rows[0]["XMLData"]
    

    Comments

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.