简体   繁体   中英

sql command in powershell script

Thanks in advance for taking the time to review this problem I have.

I am trying to streamline a process which uploads UPS tracking numbers into our ERP system. Basically, I am trying to merge all processes into a single powershell script. I am getting hung up on the sql portion to update our ERP. The sql code in itself works when run via SQL management studio as it is part of our current procedure, but when embedded in powershell, it bugs out.

Here is the basic logic:

  1. massage 4 csv files (one for each warehouse we ship from)
  2. execute 4 sql queries

In my code, I massage the 4 files which works and for testing I made several test queries just to see if sql connectivity is working. You will see select statements which actually return data.

Error:

**The 'from' keyword is not supported in this version of the language.
At C:\Users\dallan\desktop\sqltest3.ps1:56 char:8
+    FROM <<<<  'D:`\Work`\COSTCA`\CSV`\trackcal.csv'
+ CategoryInfo          : ParserError: (:) [], ParseException    
    + FullyQualifiedErrorId : FromKeywordNotAllowed**

Script:

#CALGARY


[io.file]::readalltext("`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackcalgp.csv").replace("89400","0089400") | Out-File "`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackcal1.csv" -Encoding ascii –Force
[io.file]::readalltext("`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackcal1.csv").replace(".0","") | Out-File "`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackcal2.csv" -Encoding ascii –Force
[io.file]::readalltext("`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackcal2.csv").replace('"',"") | Out-File "`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackcal.csv" -Encoding ascii –Force
$text = [IO.File]::ReadAllText("`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackcal.csv")
[IO.File]::WriteAllText("`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackcal.csv", $text.TrimEnd())



#MONTREAL

[io.file]::readalltext("`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackmtlgp.csv").replace("89400","0089400") | Out-File "`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackmtl1.csv" -Encoding ascii –Force
[io.file]::readalltext("`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackmtl1.csv").replace(".0","") | Out-File "`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackmtl2.csv" -Encoding ascii –Force
[io.file]::readalltext("`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackmtl2.csv").replace('"',"") | Out-File "`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackmtl.csv" -Encoding ascii –Force
$text = [IO.File]::ReadAllText("`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackmtl.csv")
[IO.File]::WriteAllText("`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackmtl.csv", $text.TrimEnd())

#TORONTO

[io.file]::readalltext("`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\tracktorgp.csv").replace("89400","0089400") | Out-File "`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\tracktor1.csv" -Encoding ascii –Force
[io.file]::readalltext("`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\tracktor1.csv").replace(".0","") | Out-File "`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\tracktor2.csv" -Encoding ascii –Force
[io.file]::readalltext("`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\tracktor2.csv").replace('"',"") | Out-File "`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\tracktor.csv" -Encoding ascii –Force
$text = [IO.File]::ReadAllText("`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\tracktor.csv")
[IO.File]::WriteAllText("`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\tracktor.csv", $text.TrimEnd())

#VANCOUVER

[io.file]::readalltext("`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackvangp.csv").replace("89400","0089400") | Out-File "`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackvan1.csv" -Encoding ascii –Force
[io.file]::readalltext("`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackvan1.csv").replace(".0","") | Out-File "`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackvan2.csv" -Encoding ascii –Force
[io.file]::readalltext("`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackvan2.csv").replace('"',"") | Out-File "`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackvan.csv" -Encoding ascii –Force
$text = [IO.File]::ReadAllText("`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackvan.csv")
[IO.File]::WriteAllText("`\`\DYNAMICS`\D`$`\Work`\COSTCA`\CSV`\trackvan.csv", $text.TrimEnd())



#UPLOAD

$SQLServer = "DYNAMICS" #use Server`\Instance for named SQL instances! 
$SQLDBName = "RDOT"
$SqlQuery1 = @"
CREATE TABLE #Tmp_TRACK
  `( 
    `[CSTPONBR`] `[char`]`(21`) NOT NULL,
    `[Tracking_Number`] `[char`]`(41`) NOT NULL
  `)
     CREATE TABLE #Tmp_TRACK1
  `( 
    `[SOPNUMBE`] `[char`]`(21`) NOT NULL,
    `[CSTPONBR`] `[char`](21) NOT NULL,
    `[Tracking_Number`] `[char`]`(41`) NOT NULL
  `)

BULK INSERT #Tmp_TRACK
   FROM 'D:`\Work`\COSTCA`\CSV`\trackcal.csv' 
   WITH `(
      FIRSTROW = 2,
      DATAFILETYPE='char',
      FIELDTERMINATOR=','
   `); 

INSERT INTO #Tmp_TRACK1
    SELECT 
        SOP10100.SOPNUMBE,
        #Tmp_TRACK.CSTPONBR,
        #Tmp_TRACK.Tracking_Number
    FROM 
        SOP10100 INNER JOIN
        #Tmp_TRACK ON
        SOP10100.CSTPONBR = #Tmp_TRACK.CSTPONBR AND
        SOP10100.SOPTYPE = 3

INSERT INTO SOP10107 `(SOPNUMBE, SOPTYPE, Tracking_Number`) 
    SELECT SOPNUMBE, 3, TRACKING_NUMBER FROM #Tmp_TRACK1

select * from sop10107 where sopnumbe in `(SELECT SOPNUMBE from SOP10100 where SOPTYPE = 3 and CUSTNMBR = 'COSTCA' and DOCDATE = CONVERT`(Datetime, FLOOR(CONVERT`(float,GETDATE`(`)`)`)`)`)

drop table #TMP_TRACK

drop table #TMP_TRACK1
"@

$SqlQuery2 = "select * from IV00101 where ITEMNMBR = 'B16P'"
$SqlQuery3 = "select * from IV00101 where ITEMNMBR = 'B16P'"
$SqlQuery4 = "select * from IV00101 where ITEMNMBR = 'B16P'"
$SqlQuery5 = "select * from IV00101 where ITEMNMBR = 'B16P'"

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = False; User ID = sa; Password = mypassword"

$SqlCmd1 = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd2 = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd3 = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd4 = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd5 = New-Object System.Data.SqlClient.SqlCommand

$SqlCmd1.CommandText = $SqlQuery1
$SqlCmd2.CommandText = $SqlQuery2
$SqlCmd3.CommandText = $SqlQuery3
$SqlCmd4.CommandText = $SqlQuery4
$SqlCmd5.CommandText = $SqlQuery5

$SqlCmd1.Connection = $SqlConnection
$SqlCmd2.Connection = $SqlConnection
$SqlCmd3.Connection = $SqlConnection
$SqlCmd4.Connection = $SqlConnection
$SqlCmd5.Connection = $SqlConnection

$SqlAdapter1 = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter2 = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter3 = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter4 = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter5 = New-Object System.Data.SqlClient.SqlDataAdapter

$SqlAdapter1.SelectCommand = $SqlCmd1
$SqlAdapter2.SelectCommand = $SqlCmd2
$SqlAdapter3.SelectCommand = $SqlCmd3
$SqlAdapter4.SelectCommand = $SqlCmd4
$SqlAdapter5.SelectCommand = $SqlCmd5

$DataSet1 = New-Object System.Data.DataSet
$DataSet2 = New-Object System.Data.DataSet
$DataSet3 = New-Object System.Data.DataSet
$DataSet4 = New-Object System.Data.DataSet
$DataSet5 = New-Object System.Data.DataSet

$SqlAdapter1.Fill($DataSet1)
$SqlAdapter2.Fill($DataSet2)
$SqlAdapter3.Fill($DataSet3)
$SqlAdapter4.Fill($DataSet4)
$SqlAdapter5.Fill($DataSet5)


$SqlConnection.Close()

#next line is a pause

$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

clear

$DataSet1.Tables[0]
$DataSet2.Tables[0]
$DataSet3.Tables[0]
$DataSet4.Tables[0]
$DataSet5.Tables[0]

As you can see I escaped lots of characters that I thought powershell would freak out about. I would not be surprised if part of the problem is related to this.

Any help is greatly appreciated!

\\ , ( / ) , and [ / ] don't need to be escaped. Take out all of that as it's somewhat confusing and will probably be error prone.

But the error you're getting indicates that the from keyword is not supported (it looks like this is in reference to the BULK INSERT statement you're using. What version of SQL server is this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM