header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Stored Procedure Help
Last Post 29 Aug 2009 07:33 PM by Chipper351. 3 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Resolved
Chipper351User is Offline
New Member
New Member
Posts:19
Avatar

--
29 Aug 2009 02:33 AM
    I am Trying to Get the Results of a SQL Stored procedure that I am feeding multiple databases to restore. I have a function that Opens up the Connection with SQL as I'm going to be passing several databases to a single query. My problem is understanding how to get the results from the Stored procedure and being able to get values from those results.


    function SQLRestoreDatabase {
        Param ($query, $connection)
           
        $sqlCommand = new-object System.Data.SqlClient.SqlCommand
        $sqlCommand.Connection= $connection
        $sqlCommand.CommandText= $query
                       
        $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
        $SqlAdapter.SelectCommand = $sqlCommand
        $DataSet = New-Object System.Data.DataSet   
        $SqlAdapter.Fill($DataSet)    
        $DataSet.Tables[0]
               
    }


    $sqlConnection = new-object System.Data.SqlClient.SqlConnection
    $sqlConnection.ConnectionString = "server=" + $SQLServer + ";integrated security=true;database=" + $SQLDatabase
    $SQLQuery = "exec dbo.restoredb"

    SQLRestoreDatabase $SQLQuery $sqlConnection

    These are the results that are returned every time I run the query.
    1. I would like to be able to pull the value out of Col3. is there any way to do this or possibly a better way to run this SQL Stored Procedure to get the results that I am looking for?
    2. Is there a way to make it so the results are not Output but I can still use them.
    3. Also if there is a better way to format the output as Format-Table doesn't seem to do anything


     Col1                   Col2             Col3                       DateTime            
      ---------         ----------       ------------         -------------------            
                 0                  0                 1                    8/29/2009 2:36:22 AM   
    Chad MillerUser is Offline
    Basic Member
    Basic Member
    Posts:160
    Avatar

    --
    29 Aug 2009 08:29 AM
    You could capture the array of DataRow returned from your function:
    $dt = SQLRestoreDatabase $SQLQuery $sqlConnection
    and then do something like this
    $col3 = $dt | foreach {$_.Col3}
    $col3[0]
    Or using a select-object
    $col3 = $dt | select Col3
    $col3[0].Col3

    I'm not sure what you mean by format-table doesn't do anything, the results you've posted are formated in table. Check get-help format-table for additional options.
    Chipper351User is Offline
    New Member
    New Member
    Posts:19
    Avatar

    --
    29 Aug 2009 06:24 PM
    EDIT: Looks Like this did not solve my issues When calling my Function

    $dt = SQLRestoreDatabase $SQLQuery $sqlConnection
    $col3 = $dt | select Col3
    $col3[0].Col3


    I get no result. This is my full code

    function SQLRestoreDatabase {
        Param ($query, $connection)
           
        $sqlCommand = new-object System.Data.SqlClient.SqlCommand
        $sqlCommand.Connection= $connection
        $sqlCommand.CommandText= $query
                       
        $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
        $SqlAdapter.SelectCommand = $sqlCommand
        $DataSet = New-Object System.Data.DataSet   
        $SqlAdapter.Fill($DataSet)    
        $DataSet.Tables[0]
               
    }


    $sqlConnection = new-object System.Data.SqlClient.SqlConnection
    $sqlConnection.ConnectionString = "server=" + $SQLServer + ";integrated security=true;database=" + $SQLDatabase
    $SQLQuery = "exec dbo.restoredb"

    $dt = SQLRestoreDatabase $SQLQuery $sqlConnection
    $col3 = $dt | select Col3
    $col3[0].Col3


    I get No result displayed. When Debugging and going through my dt variable under the SyncRoot>[1]> shows the column names and then the values next to them. This lead me to believe that the function is returning the results that I am looking for I just can't figure out the proper syntax to retrieve the information I need. Thank you!!!!

    Chipper351User is Offline
    New Member
    New Member
    Posts:19
    Avatar

    --
    29 Aug 2009 07:33 PM
    • Accepted Answer
    Looks like using your suggestion I was able to call the actual column name and return the result that I needed

    $col2 = $dt[1].DidRestore

    My File Code looks like this and returns the result that I need. The "DidRestore" is the name of the column. It would be nice if I could do this without calling the column name but this does work.

    function SQLRestoreDatabase {
        Param ($query, $connection)
           
        $sqlCommand = new-object System.Data.SqlClient.SqlCommand
        $sqlCommand.Connection= $connection
        $sqlCommand.CommandText= $query
                       
        $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
        $SqlAdapter.SelectCommand = $sqlCommand
        $DataSet = New-Object System.Data.DataSet   
        $SqlAdapter.Fill($DataSet)    
        $DataSet.Tables[0]
               
    }


    $sqlConnection = new-object System.Data.SqlClient.SqlConnection
    $sqlConnection.ConnectionString = "server=" + $SQLServer + ";integrated security=true;database=" + $SQLDatabase
    $SQLQuery = "exec dbo.restoredb"

    $dt = SQLRestoreDatabase $SQLQuery $sqlConnection
    $col3 = $dt[1].DidRestore



    Really Appreciate your time and help. I would not of been able to come to this without your help!
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer