[英]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. 我正在尝试简化将UPS跟踪号上传到我们的ERP系统的过程。 Basically, I am trying to merge all processes into a single powershell script.
基本上,我试图将所有进程合并到一个Powershell脚本中。 I am getting hung up on the sql portion to update our ERP.
我正在挂断sql部分来更新我们的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.
当通过SQL Management Studio运行时,sql代码本身可以工作,这是我们当前过程的一部分,但是当嵌入到powershell中时,它会出错。
Here is the basic logic: 这是基本逻辑:
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. 在我的代码中,我整理了4个有效的文件,并进行了一些测试查询以进行测试,以查看sql连接是否正常工作。 You will see select statements which actually return data.
您将看到实际上返回数据的select语句。
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. 如您所见,我逃脱了很多我以为Powershell会发疯的角色。 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? 但是,您得到的错误表明不支持
from
关键字(这似乎是在引用您使用的BULK INSERT
语句。这是什么版本的SQL Server?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.