Powershell 脚本与 SQL 通信,不会接受带撇号的值

问题描述 投票:0回答:1

我正在尝试自动将 CSV 中的值插入 SQL 数据库中的表中。除了带有撇号的名称之外,所有内容都很好地传输到数据库中。我尝试在第一个撇号旁边添加第二个撇号,希望它能够逃脱,但没有运气。

我收到的错误如下所示:

Exception calling "ExecuteReader" with "0" argument(s): "Incorrect syntax near 'BRIEN'.
Incorrect syntax near 'BRIEN'.
Unclosed quotation mark after the character string '
                        WHERE NPINum = (something);
                    END
            '."
At C:\REAL SCRIPTING HOURS\NPI\NPI-SQL.ps1:232 char:13
+             $SqlDataReader = $SqlCommand.ExecuteReader();
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SqlException

给我带来问题的部分看起来像这样(请原谅混乱):

$csvData = Import-CSV -Path $moveToSQL.Item($iterator)
        $correctCols = $csvData | Select-Object "NPI","Provider Last Name (Legal Name)", "Provider First Name", "Provider Middle Name", "Provider Credential Text"
        ##Get it into SQL
        for($i = 0; $i -lt $correctCols.Count; $i++) {
            
            ##Make sure there are no issues with apostrophe'd names (I know there's likely a more visually appealing way of doing this... but alas)
            $apostropheCheck = 0

            ##Check the last name listed
            $apostropheCheck =$correctCols.Get($i)."Provider Last Name (Legal Name)".Contains("'")    #bool to see if an apostrophe even exists
            if($apostropheCheck -eq "True") {                                                         #if it does, do something
                $apostropheCheck=$correctCols.Get($i)."Provider Last Name (Legal Name)".IndexOf("'")  #get the index of where the apostrophe is
                $correctCols.Get($i)."Provider Last Name (Legal Name)".Insert($apostropheCheck,"'")   #add in another one for good measure (break out)
            }                                                                                         #rinse and repeat

            ##Check the first name listed
            $apostropheCheck =$correctCols.Get($i)."Provider First Name".Contains("'")
            if($apostropheCheck -eq "True") {
                $apostropheCheck=$correctCols.Get($i)."Provider First Name".IndexOf("'")
                $correctCols.Get($i)."Provider First Name".Insert($apostropheCheck,"'")
            }

            ##Check the middle name listed
            $apostropheCheck =$correctCols.Get($i)."Provider Middle Name".Contains("'")
            if($apostropheCheck -eq "True") {
                $apostropheCheck=$correctCols.Get($i)."Provider Middle Name".IndexOf("'")
                $correctCols.Get($i)."Provider Middle Name".Insert($apostropheCheck,"'")
            }

            ##Create query to run
            $SqlQuery = "
            IF NOT EXISTS (SELECT 1 FROM (my.dbo.table) WHERE NPINum = {0})
                BEGIN
                    INSERT INTO (my.dbo.table)
                        VALUES 
                        ('{0}' 
                        ,'{1}' 
                        ,'{2}'
                        ,'{3}'
                        ,'{4}')
                END
            ELSE
                IF NOT EXISTS 
                        (SELECT 1 FROM (my.dbo.table) 
                        WHERE NPINum = {0} AND LastName = '{1}' 
                        AND FirstName = '{2}' 
                        AND MiddleName = '{3}' 
                        AND Credentials = '{4}')                    
                    BEGIN
                        UPDATE (my.dbo.table) 
                        SET LastName = '{1}', FirstName = '{2}', MiddleName = '{3}', Credentials = '{4}'
                        WHERE NPINum = {0};
                    END
            " -f $correctCols.Get($i).NPI, $correctCols.Get($i)."Provider Last Name (Legal Name)", $correctCols.Get($i)."Provider First Name", $correctCols.Get($i)."Provider Middle Name", $correctCols.Get($i)."Provider Credential Text"
            $SqlCommand.CommandText = $SqlQuery;
            ##Open up SQL communication and execute query in the database
            $SqlConnection.Open();
            $SqlDataReader = $SqlCommand.ExecuteReader();
            $SqlConnection.Close();
        }
sql powershell
1个回答
0
投票

您尝试过 ''''' 五个单撇号吗?

© www.soinside.com 2019 - 2024. All rights reserved.