0

Is there a way to save sql connection details and re-use them in the same script. I've written the below that exports the contents of 3 sql tables on the same database. It works but it re-uses a load of code and I can't figure out how to reduce it.

I've tried declaring all the connection information and then stating it where the full code is now, but that just outputs the first table 3 times.

I've also tried stating it once at the beginning which again didn't work.

#agent_lookup

$server = "test"
$database = "MI_Lookups"
$query = "EXEC [dbo].[proc_update_new_agents]
          SELECT  [l].agent_key AS 'Agent_Key'
                  ,ISNULL([l].agent_name,'') AS 'Agent_Name'
                  ,ISNULL([l].agent_name_1,'') AS 'Agent_Name_1'
                  ,ISNULL([l].agent_type,'') AS 'Agent_Type'
          FROM [dbo].[agent_test] AS [l]
          ORDER by [l].agent_key asc"

$connection = New-Object System.Data.SqlClient.SqlConnection("Data Source=$server;Integrated Security=SSPI;Initial Catalog=$database;")
$command = New-Object System.Data.SqlClient.SqlCommand($query, $connection)
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($command)
$DataSet = New-Object System.Data.DataSet

$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0] | Export-Csv $saveloaction\"Agent_Lookup.csv"
$connection.Close()

#organisation_lookup

$server = "test"
$database = "MI_Lookups"
$query = "EXEC [dbo].[proc_update_new_organisations]
          SELECT  [l].organisation_key AS 'Organisation_Key'
                  ,ISNULL([l].broker_name,'') AS 'Broker_Name'
                  ,ISNULL([l].team_member,'') AS 'Team_Member'
                  ,ISNULL([l].team_leader,'') AS 'Team_Leader'
          FROM [dbo].[orgnisation_test] as [l]
          ORDER BY [l].organisation_key asc"

$connection = New-Object System.Data.SqlClient.SqlConnection("Data Source=$server;Integrated Security=SSPI;Initial Catalog=$database;")
$command = New-Object System.Data.SqlClient.SqlCommand($query, $connection)
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($command)
$DataSet = New-Object System.Data.DataSet

$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0] | Export-Csv $saveloaction\"Organisation_Lookup.csv"
$connection.Close()

#insurer_lookup

$server = "test"
$database = "MI_Lookups"
$query = "EXEC [dbo].[proc_update_new_insurers]
          SELECT  [l].insurer_key AS 'Insurer_Key'
                  ,ISNULL([l].insurer_name,'') AS 'Insurer_Name'
                  ,ISNULL([l].insurer_name_1,'') AS 'Insurer_Name_1'
                  ,ISNULL([l].team_member,'') AS 'Team_Member'
                  ,ISNULL([l].team_leader,'') AS 'Team_Leader'
          FROM [dbo].[insurer_test] AS [l]
          ORDER BY [l].insurer_key asc"

$connection = New-Object System.Data.SqlClient.SqlConnection("Data Source=$server;Integrated Security=SSPI;Initial Catalog=$database;")
$command = New-Object System.Data.SqlClient.SqlCommand($query, $connection)
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($command)
$DataSet = New-Object System.Data.DataSet

$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0] | Export-Csv $saveloaction\"Insurer_Lookup.csv"
$connection.Close()

It would seem logical to me to only need to write the connection information once, but I'm new to powershell and struggling to make this as simple as I'm sure it should be. The above is basically the same script repeated three times with a different table referenced.

Thanks as always.

2 Answers 2

1

I would use a function to handle the execution of the query. That way you only need to change the query and the place to store the output.

Note that I've got the database details as global variables here. This is fine if you're using just this one script but is best avoided if this script is calling, or being called by other scripts.

function Query-ToCsv($query, $csvLocation) {

    try {
        $command = New-Object System.Data.SqlClient.SqlCommand($query, $connection)
        $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($command)
        $DataSet = New-Object System.Data.DataSet
        $SqlAdapter.Fill($DataSet)
        $DataSet.Tables[0] | Export-Csv $csvLocation
    }
    catch {
        Write-Warning "Exception: " + $_.Exception.Message
    }
}

# script globals
$server = "test"
$database = "MI_Lookups"
$connection = New-Object System.Data.SqlClient.SqlConnection("Data Source=$server;Integrated Security=SSPI;Initial Catalog=$database;")

#agent_lookup
$query = "EXEC [dbo].[proc_update_new_agents]
          SELECT  [l].agent_key AS 'Agent_Key'
                  ,ISNULL([l].agent_name,'') AS 'Agent_Name'
                  ,ISNULL([l].agent_name_1,'') AS 'Agent_Name_1'
                  ,ISNULL([l].agent_type,'') AS 'Agent_Type'
          FROM [dbo].[agent_test] AS [l]
          ORDER by [l].agent_key asc"

Query-ToCsv $query "$saveloaction\Agent_Lookup.csv"

#organisation_lookup
$query = "EXEC [dbo].[proc_update_new_organisations]
          SELECT  [l].organisation_key AS 'Organisation_Key'
                  ,ISNULL([l].broker_name,'') AS 'Broker_Name'
                  ,ISNULL([l].team_member,'') AS 'Team_Member'
                  ,ISNULL([l].team_leader,'') AS 'Team_Leader'
          FROM [dbo].[orgnisation_test] as [l]
          ORDER BY [l].organisation_key asc"


Query-ToCsv $query "$saveloaction\Organisation_Lookup.csv"

#insurer_lookup
$query = "EXEC [dbo].[proc_update_new_insurers]
          SELECT  [l].insurer_key AS 'Insurer_Key'
                  ,ISNULL([l].insurer_name,'') AS 'Insurer_Name'
                  ,ISNULL([l].insurer_name_1,'') AS 'Insurer_Name_1'
                  ,ISNULL([l].team_member,'') AS 'Team_Member'
                  ,ISNULL([l].team_leader,'') AS 'Team_Leader'
          FROM [dbo].[insurer_test] AS [l]
          ORDER BY [l].insurer_key asc"

Query-ToCsv $query "$saveloaction\Insurer_Lookup.csv"

$connection.Close()

Note that this is untested (obviously, as I don't have access to your database) but should get you going in the right direction.

Sign up to request clarification or add additional context in comments.

1 Comment

Genius! I was able to implement this perfectly. I even managed to re-use the try/catch in another element of the script, with the results being appended to a log file. Thank you.
0

You can simply reuse the connection object. I've edited your code to do this but I haven't run it - excuse me if I've done some bad editing but the gist of it should be right.

$server = "test"
$database = "MI_Lookups"
$connection = New-Object System.Data.SqlClient.SqlConnection("Data Source=$server;Integrated Security=SSPI;Initial Catalog=$database;")

#agent_lookup

$query = "EXEC [dbo].[proc_update_new_agents]
          SELECT  [l].agent_key AS 'Agent_Key'
                  ,ISNULL([l].agent_name,'') AS 'Agent_Name'
                  ,ISNULL([l].agent_name_1,'') AS 'Agent_Name_1'
                  ,ISNULL([l].agent_type,'') AS 'Agent_Type'
          FROM [dbo].[agent_test] AS [l]
          ORDER by [l].agent_key asc"

$command = New-Object System.Data.SqlClient.SqlCommand($query, $connection)
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($command)
$DataSet = New-Object System.Data.DataSet

$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0] | Export-Csv $saveloaction\"Agent_Lookup.csv"

#organisation_lookup

$query = "EXEC [dbo].[proc_update_new_organisations]
          SELECT  [l].organisation_key AS 'Organisation_Key'
                  ,ISNULL([l].broker_name,'') AS 'Broker_Name'
                  ,ISNULL([l].team_member,'') AS 'Team_Member'
                  ,ISNULL([l].team_leader,'') AS 'Team_Leader'
          FROM [dbo].[orgnisation_test] as [l]
          ORDER BY [l].organisation_key asc"

$command = New-Object System.Data.SqlClient.SqlCommand($query, $connection)
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($command)
$DataSet = New-Object System.Data.DataSet

$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0] | Export-Csv $saveloaction\"Organisation_Lookup.csv"

#insurer_lookup

$query = "EXEC [dbo].[proc_update_new_insurers]
          SELECT  [l].insurer_key AS 'Insurer_Key'
                  ,ISNULL([l].insurer_name,'') AS 'Insurer_Name'
                  ,ISNULL([l].insurer_name_1,'') AS 'Insurer_Name_1'
                  ,ISNULL([l].team_member,'') AS 'Team_Member'
                  ,ISNULL([l].team_leader,'') AS 'Team_Leader'
          FROM [dbo].[insurer_test] AS [l]
          ORDER BY [l].insurer_key asc"

$command = New-Object System.Data.SqlClient.SqlCommand($query, $connection)
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($command)
$DataSet = New-Object System.Data.DataSet

$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0] | Export-Csv $saveloaction\"Insurer_Lookup.csv"

$connection.Close()

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.