 |
|
รบกวนช่วยเช็ค query ให้หน่อยครับ ผม join ผิดพลาดตรงไหนครับ |
|
 |
|
|
 |
 |
|
ช่วยตรวจสอบ query ให้หน่อยครับว่าผม join ผิดกันหรือป่าวครับ จาก query ผมเอาไปรันบน db สามารถแสดงผลได้ครับ แต่พอมาใช้ใน vb ขึ้น error แบบนี้ครับ ORA-00933: SQL command not properly ended อันนี้คือ error ที่เกิดจาก 2 ตารางนี้มา join กัน ผมต้องการให้ id ของ 2 ตาราง join กัน แล้วแสดงผล sum ของ ตาราง tr_hist ครับ (tr_qty_loc) ของเดิม จะแสดงผลรวมของ ตาราง in_mstr (in_qty_oh)
Code (VB.NET)
strSql.Append("select tr_qty_loc,in_qty_oh,in_qty_ord,in_qty_all from qad.in_mstr inner join")
strSql.Append("qad.tr_hist on Upper(tr_part) = Upper(in_part) ")
strSql.Append("where Upper(in_domain)='" & Domain & "' ")
strSql.Append("and Upper(in_part) = '" & ps_comp & "' and upper(in_site) = '" & Site & "' ")
strSql.Append("and (Upper(tr_loc) = '3205' or upper(tr_loc) Like '2%')")
อันนี้ของเดิมครับ ที่ ตาราง in_mstr (in_qty_oh) sum ผลมา (อันนี้ใช้งานได้ปกติ)
Code (VB.NET)
strSql.Append("select in_qty_oh,in_qty_ord,in_qty_all from qad.in_mstr where Upper(in_domain)='" & Domain & "' ")
strSql.Append("and Upper(in_part) = '" & ps_comp & "' and upper(in_site) = '" & Site & "' ")
query ใน oracle
Code (VB.NET)
select sum(tr_qty_loc) , min (in_qty_oh) ,min( in_qty_ord ), min(in_qty_all) from qad.in_mstr a inner join
qad.tr_hist on Upper(in_part)= Upper(tr_part)
and Upper(in_site)= Upper(tr_site)
and Upper(in_domain)= Upper(tr_domain)
where Upper(in_part) = '13750217'
and Upper(in_domain) = '2000' and Upper(in_site) = '2100'
and (Upper(tr_loc) = '3205' or upper(tr_loc) Like '2%')
--group by Upper(in_part)
Tag : .NET, Oracle, VS 2008 (.NET 3.x)
|
ประวัติการแก้ไข 2018-01-23 11:45:48
|
 |
 |
 |
 |
Date :
2018-01-23 11:22:19 |
By :
bankguszo |
View :
1084 |
Reply :
13 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเอาค่า strSql ออกมาเป็นคำสังดูครับ อาจมีการเว้นวรรคไม่ถูกต้อง
|
 |
 |
 |
 |
Date :
2018-01-23 11:47:34 |
By :
OOP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองแก้ดูแล้วครับตรง strSql กลับกลายเป็นขึ้นคำว่า ORA-00905: missing keyword แทนครับ แสดงว่าตอนแรก strsql ไม่ถูก
|
 |
 |
 |
 |
Date :
2018-01-23 13:14:40 |
By :
bankguszo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
var result=strSql.ToString ดูค่ามันอะครับ
|
 |
 |
 |
 |
Date :
2018-01-23 13:16:03 |
By :
OOP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมเอาไปรันดูบน db แล้วครับ 2ตาราง join กัน ได้ค่าว่าง แต่พอ select แยกตารางกลับเจอค่า จะเป็นเฉพาะบางเลขครับ (in_part) บางเลข ก็เจอ
|
 |
 |
 |
 |
Date :
2018-01-23 13:19:19 |
By :
bankguszo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ครับ อันนั้นไม่ใช่ประเด็น ถ้า JOIN กัน เจอไม่เจออยู่ที่เงื่อนไขครับ ปัญหาคือ แก้ Error ได้ยังครับ
|
 |
 |
 |
 |
Date :
2018-01-23 13:23:33 |
By :
OOP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แก้ไขได้แล้วครับแต่ก็มีอีกปัญหาคือ มันหา dataset ไม่เจอครับ ติดตั้งแต่ตัวแรกเลยครับ (in_qty_oh)
Code (VB.NET)
Dim GetqtyDS As DataSet = New DataSet
GetqtyDS = Getqty(dm("ps_comp"), Session("domain"), Session("site"))
If GetqtyDS.Tables(0) IsNot Nothing AndAlso GetqtyDS.Tables(0).Rows.Count > 0 Then
qty_oh = IIf(IsDBNull(GetqtyDS.Tables(0).Rows.Item(0).Item("in_qty_oh")) = True, "0", GetqtyDS.Tables(0).Rows.Item(0).Item("in_qty_oh"))
qty_ord = IIf(IsDBNull(GetqtyDS.Tables(0).Rows.Item(0).Item("in_qty_ord")) = True, "0", GetqtyDS.Tables(0).Rows.Item(0).Item("in_qty_ord"))
qty_all = IIf(IsDBNull(GetqtyDS.Tables(0).Rows.Item(0).Item("in_qty_all")) = True, "0", GetqtyDS.Tables(0).Rows.Item(0).Item("in_qty_all"))
qty_loc = IIf(IsDBNull(GetqtyDS.Tables(0).Rows.Item(0).Item("tr_qty_loc")) = True, "0", GetqtyDS.Tables(0).Rows.Item(0).Item("tr_qty_loc"))
Else
qty_oh = 0
qty_ord = 0
qty_all = 0
qty_loc = 0
End If
Code (VB.NET)
Public Shared Function Getqty(ByVal ps_comp As String, ByVal Domain As String, ByVal Site As String) As DataSet
Try
strSql = New StringBuilder()
strSql.Append("select min(in_qty_oh) ,min( in_qty_ord ), min(in_qty_all) , sum(tr_qty_loc) from qad.in_mstr a")
strSql.Append("inner join qad.tr_hist on Upper(tr_part)= Upper(in_part) ")
strSql.Append("and Upper(in_site)= Upper(tr_site)")
strSql.Append("and Upper(in_domain)= Upper(tr_domain)")
strSql.Append("where Upper(in_part) = '" & ps_comp & "' ")
strSql.Append("and (Upper(in_domain) = '" & Domain & "' and Upper(in_site) = '" & Site & "')")
strSql.Append("and (Upper(tr_loc) = '3205' or Upper(tr_loc) Like '2%')")
Dim result = strSql.ToString
Return GetDataOracle(strSql.ToString, "Getqty", constrdata)
Catch ex As Exception
Throw ex
End Try
End Function
|
 |
 |
 |
 |
Date :
2018-01-24 09:24:10 |
By :
bankguszo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอดู Error หน่อยครับ แล้วอันบนแก้ไงครับ
|
 |
 |
 |
 |
Date :
2018-01-24 09:47:29 |
By :
oop |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองดูดีๆ ก่อนนะครับ ผมมองด้วยตาปล่าวยังรู้เลยครับว่าคุณเว้นวรรคไม่ถูก ลองทำตามวิธีที่ผมแนะนนำครับ เอาข้ันตอนเว้นวรรคให้ได้ก่อน ค่อยไปอันอื่นคับ
|
 |
 |
 |
 |
Date :
2018-01-24 09:56:09 |
By :
OOP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ชัวร์นะครับ ว่าขึ้นอะ เพราะผมมองแล้วมันผิดหลายบรรทัดเลยนะ
|
 |
 |
 |
 |
Date :
2018-01-24 10:45:51 |
By :
OOP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตามนี้เลยครับ ที่ข้อมูลมันว่างๆคือ เลข in_part นี้ ไม่มีข้อมูลครับ. ถ้าใส่เลขที่มีข้อมูลก็ขึ้นปกติครับ แต่ผมกลับไปใช้ query ที่เคยใช้ก่อนหน้านี้มันเห็น dataset ครับ. table ก็ใช้อันเดิมแค่เพิ่มอีกอันมา join ครับ


|
 |
 |
 |
 |
Date :
2018-01-24 11:14:19 |
By :
bankguszo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สาเหตุที่ไม่ออกเพราะว่า
select min(in_qty_oh) ,min( in_qty_ord ), min(in_qty_all) , sum(tr_qty_loc) from qad.in_mstr a
ต้องทำเป็น
select min(in_qty_oh) AS in_qty_oh แบบนี้ให้ครบทุกตัวที่เราจะดึงมาให้ครับ
|
 |
 |
 |
 |
Date :
2018-01-24 11:50:13 |
By :
OOP |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วครับขอบคุณครับผม 
|
 |
 |
 |
 |
Date :
2018-01-24 15:45:16 |
By :
bankguszo |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|