How to loop over the result (system.Data.DataSet object) of SQL run from PowerShell

瘦欲@ 提交于 2019-12-11 17:37:04

问题


I am trying to run a SQL from Power Shell(which is on my windows 7 64 bit desktop) and the remote database host is MS SQL Server 2012.

I am running SQL1 by calling function Get-ODBC-Data which will give me single column of type string. It can have unknown number of rows (up to 20). Then I am using each of these column values as parameter ($var1) to the second function Get-ODBC-Data-Count. The SQL2 in this function Get-ODBC-Data-Count will give me count using $var1 in where clause.

The code is:

        function Get-ODBC-Data{       
           param(
           [string]$query=$('
                        SELECT col3
                        FROM [master].[sys].[table_name]'),         
           [string]$username='db_user_name',
           [string]$password='db_password'
           )
           $conn = New-Object System.Data.Odbc.OdbcConnection
           $conn.ConnectionString = "DRIVER={SQL Server};Server=123.456.78.90;Initial Catalog=master;Uid=$username;Pwd=$password;"
           $conn.open()
           $cmd = New-object System.Data.Odbc.OdbcCommand($query,$conn)
           $ds = New-Object system.Data.DataSet
           (New-Object system.Data.odbc.odbcDataAdapter($cmd)).fill($ds) | out-null
           $conn.close()
           $ds.Tables[0]
        }

        function Get-ODBC-Data-Count{
           [parameter(Mandatory=$true)][string]$var1,
           param(
           [string]$query=$('
                        SELECT COUNT(*)
                        FROM [master].[sys].[table_name]                
                                        WHERE col2 = '$($var1)'
                                        ;
        '),
           [string]$username='db_user_name',
           [string]$password='db_password'
           )
           $conn = New-Object System.Data.Odbc.OdbcConnection
           $conn.ConnectionString = "DRIVER={SQL Server};Server=123.456.78.90;Initial Catalog=master;Uid=$username;Pwd=$password;"
           $conn.open()
           $cmd = New-object System.Data.Odbc.OdbcCommand($query,$conn)
           $ds = New-Object system.Data.DataSet
           (New-Object system.Data.odbc.odbcDataAdapter($cmd)).fill($ds) | out-null
           $conn.close()
           $ds.Tables[0]
        }

$result = Get-ODBC-Data

$count_1 = Get-ODBC-Data-Count -var1 $result[0][0]
Write-Host "`$count_1[0]:" $count_1[0];
Write-Host "Message: Count of " $result[0][0] " is" $count_1[0];

$count_2 = Get-ODBC-Data-Count -var1 $result[1][0]
Write-Host "`$count_2:" $count_2[0];
Write-Host "Message: Count of " $result[1][0] " is" $count_2[0];

$count_3 = Get-ODBC-Data-Count -var1 $result[2][0]
Write-Host "`$count_3:" $count_3[0];
Write-Host "Message: Count of " $result[2][0] " is" $count_3[0];

This code works if I know number of rows in the result of SQL1.

My Question: How can I modify this code so unknown number of rows in result of SQL1 will be handled and I can call function Get-ODBC-Data-Count for each row of SQL1?


回答1:


There are a lot of issues here. You're building SQL strings. Don't do this! Use SQL parameters instead! You're repeating a lot of code unessescarily. You're using Data Tables, which I would avoid, at least in powershell. You're not re-using the database connection.

Always try really hard to avoid loops with a query inside when working with SQL. Try and think if you can rewrite the SQL instead.

Try this SQL:

SELECT 
col2,
COUNT(<thePrimaryKeyColumnOfTheTable>)
FROM [master].[sys].[table_name]
GROUP BY col2

That should give you the count of all the different values of col2.



来源:https://stackoverflow.com/questions/50418878/how-to-loop-over-the-result-system-data-dataset-object-of-sql-run-from-powersh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!