How to compare csv file with sql database using PowerShell

I've latest data in my CSV file, I'm trying to compare to my SQL Server Database Example1 table that contain old data using the EmpId and Name columns. If there is new data in the csv file then update Example1 table and insert only the new data into Example2 table.

I'm beginner and I'm stuck with the logic part so I would be really appreciated if I can get any help or suggestion. Any guidance would be really appreciated.

My CSV file, Example1 and Example2 database tables look like this

EmpId    Name           Hold
324   John Smith     Yes
432   Tim Cook       Yes
        $csv_file = "C:\scripts\user-info.csv"
        
        $csvImport = import-csv $csv_file

        foreach ($i in $csvImport){
              $EmpId = $i.EmpId
              $Name = $i.Name
              $Hold = $i.Hold

        }


        $Server = 'Server'
        $query = "SELECT EmpId,Name FROM Example1"
        $Database = 'DatabaseName'
        $result = Invoke-Sqlcmd -Query $query -ServerInstance $Server -Database $Database  | Select-Object * -ExcludeProperty ItemArray, Table, RowError, RowState, HasErrors



Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)