Incorrect SQL syntax pointing at empty bottom row - sql-server
I have a set of data that I upload to SQL and since a couple of new columns have been added (abandoned IVR & answered IVR) it is failing to upload properly.
The data will now only land in the temp table and not the full data one, but what makes it all the more confusing is that it's saying their is a syntax error of ',' on the bottom line which is blank
This is my merge statement which is when I debug is saying where the potential error is. But I don't understand why the error is pointing at a blank row and stopping the upload from happening?
Could anyone give me any pointers, apologies for the code layout - I don't know how to set it out.
Thanks
Dan
sSQL = "MERGE [C3 ODS].Telephony AS Target USING (SELECT CallDate,VDN,VDN_Name,Skill,Skill_Name,Telephony_Lookup,Site,Team,SubTeam,Client, '" & _
" Scheme,Calls_Offered,Calls_Answered,Calls_Abandoned,Calls_Abandoned_SLA,Calls_Answered_SLA,Average_TalkTime,Average_ACW,Average_HandleTime, '" & _
" Average_AbandonTime,Average_SpeedAnswer,TelephonySystem,TimePeriod1,TimePeriod2,TimePeriod3,TimePeriod4,TimePeriod5,TimePeriod6,TimePeriod7, '" & _
" TimePeriod8,TimePeriod9,Abandon1,Abandon2,Abandon3,Abandon4,Abandon5,Abandon6,Abandon7,Abandon8,Abandon9,Abandon10,Answered1,Answered2, '" & _
" Answered3,Answered4,Answered5,Answered6,Answered7,Answered8,Answered9,Answered10,DataLoad_Date,ClientGroup,LOB,ServiceLevel,Section, '" & _
" ForecastGroup,AbandonedIVR,AnsweredIVR FROM [C3 ODS].Telephony_Temp) AS Temp " & _
" ON Target.CallDate = Temp.CallDate AND Target.Telephony_Lookup = Temp.Telephony_Lookup WHEN MATCHED THEN UPDATE SET Target.Calls_Offered = Temp.Calls_Offered, '" & _
" Target.Calls_Answered = Temp.Calls_Answered, Target.Calls_Abandoned = Temp.Calls_Abandoned, Target.Calls_Abandoned_SLA = Temp.Calls_Abandoned_SLA, '" & _
" Target.Calls_Answered_SLA = Temp.Calls_Answered_SLA, Target.Average_TalkTime = Temp.Average_TalkTime, Target.Average_ACW = Temp.Average_ACW, '" & _
" Target.Average_HandleTime = Temp.Average_HandleTime, Target.Average_AbandonTime = Temp.Average_AbandonTime, Target.Average_SpeedAnswer = Temp.Average_SpeedAnswer, '" & _
" Target.VDN_Name = Temp.VDN_Name, Target.Skill_Name = Temp.Skill_Name, Target.Site = Temp.Site, Target.Team = Temp.Team, Target.SubTeam = Temp.SubTeam, '" & _
" Target.ClientGroup = Temp.ClientGroup, Target.Client = Temp.Client, Target.Scheme = Temp.Scheme " & _
" WHEN NOT MATCHED THEN INSERT (CallDate,VDN,VDN_Name,Skill,Skill_Name,Telephony_Lookup,Site,Team,SubTeam,Client,Scheme,Calls_Offered,Calls_Answered, '" & _
" Calls_Abandoned,Calls_Abandoned_SLA,Calls_Answered_SLA,Average_TalkTime,Average_ACW,Average_HandleTime,Average_AbandonTime,Average_SpeedAnswer, '" & _
" TelephonySystem,TimePeriod1,TimePeriod2,TimePeriod3,TimePeriod4,TimePeriod5,TimePeriod6,TimePeriod7,TimePeriod8,TimePeriod9,Abandon1,Abandon2, '" & _
" Abandon3,Abandon4,Abandon5,Abandon6,Abandon7,Abandon8,Abandon9,Abandon10,Answered1,Answered2,Answered3,Answered4,Answered5,Answered6,Answered7, '" & _
" Answered8,Answered9,Answered10,DataLoad_Date,ClientGroup,LOB,ServiceLevel,Section,ForecastGroup,AbandonedIVR,AnsweredIVR) " & _
" VALUES (Temp.CallDate, Temp.VDN, Temp.VDN_Name, Temp.Skill, Temp.Skill_Name, Temp.Telephony_Lookup, Temp.Site, Temp.Team, Temp.SubTeam, '" & _
" Temp.Client, Temp.Scheme, Temp.Calls_Offered, Temp.Calls_Answered, Temp.Calls_Abandoned, Temp.Calls_Abandoned_SLA, Temp.Calls_Answered_SLA, '" & _
" Temp.Average_TalkTime, Temp.Average_ACW, Temp.Average_HandleTime, Temp.Average_AbandonTime, Temp.Average_SpeedAnswer, Temp.TelephonySystem, '" & _
" Temp.TimePeriod1, Temp.TimePeriod2, Temp.TimePeriod3, Temp.TimePeriod4, Temp.TimePeriod5, Temp.TimePeriod6, Temp.TimePeriod7, Temp.TimePeriod8, '" & _
" Temp.TimePeriod9, Temp.Abandon1, Temp.Abandon2, Temp.Abandon3, Temp.Abandon4, Temp.Abandon5, Temp.Abandon6, Temp.Abandon7, Temp.Abandon8, '" & _
" Temp.Abandon9, Temp.Abandon10, Temp.Answered1, Temp.Answered2, Temp.Answered3, Temp.Answered4, Temp.Answered5, Temp.Answered6, Temp.Answered7, '" & _
" Temp.Answered8, Temp.Answered9, Temp.Answered10, Temp.DataLoad_Date, Temp.ClientGroup, Temp.LOB, Temp.ServiceLevel, Temp.Section, '" & _
" Temp.ForecastGroup, Temp.AbandonedIVR, Temp.AnsweredIVR);"
Cn.Execute sSQL
Related
Allow Blank Entries in Range Excel?
Below is the code. What I'm confused about is how to get the BuyOne, BuyTwo, BuyThree, BuyFour array (arrBuyer) to allow blank entries. Right now if an entry is blank it'll say it's an invalid entry, whereas I just want it skipped over. In addition, if ALL entries are blank, I want it where the code just assumes the user wants to run every possible entry. The user enters codes into four specific slots on the left of the page as seen in the image. Currently the code checks a list off to the right of the page to see if values match. How do I go about doing the above? If Range("reqFrDt") = vbNullString Or IsDate(Range("reqFrDt")) = False Then MsgBox "From date missing..." Range("reqFrDt").Select End Else sFrDt = Range("reqFrDt") End If If Range("reqToDt") = vbNullString Or IsDate(Range("reqToDt")) = False Then MsgBox "To date missing or invalid.." Range("reqToDt").Select End Else sToDt = Range("reqToDt") End If lastRow = Range("O" & Rows.Count).End(xlUp).Row Set rBuyerList = Range("O1:O" & lastRow) arrBuyer = Array("BuyOne", "BuyTwo", "BuyThree", "BuyFour") For i = 0 To UBound(arrBuyer) With Application chkFind = .IfError(.Match(Range(arrBuyer(i)), Range("O1:O50"), 0), 0) End With If Range(arrBuyer(i)) = vbNullString Or chkFind = False Then MsgBox "Invalid Buyer Code.." & arrBuyer(i) Range(arrBuyer(i)).Select End If Next i Call runFinished(sFrDt, sToDt, arrBuyer) Sheets("Main Sheet").Select MsgBox ("done...") End Sub Sub runFinished(sFrDt As String, sToDt As String, arrBuyer As Variant) Dim SQL As String ' add a new work sheet ActiveWorkbook.Worksheets.Add ' dispay Criteria Cells(1, 1) = "Run Date: " & Now() Call MergeLeft("A1:B1") Cells(2, 1) = "Criteria:" Cells(2, 2) = "From " & Range("reqFrDT") & " -To- " & Range("reqToDt") ' SQL SQL = "select a.StockCode [Finished Part], a.QtyToMake, FQOH,FQOO,/*FQIT,*/FQOA, b.Component [Base Material], CQOH,CQOO,CQIT,CQOA " & _ "from ( " & _ " SELECT StockCode, sum(QtyToMake) QtyToMake " & _ " from [MrpSugJobMaster] " & _ " WHERE 1 = 1 " & _ " AND JobStartDate >= '" & sFrDt & "' " & _ " AND JobStartDate <= '" & sToDt & "' " & _ " AND JobClassification = 'OUTS' " & _ " AND ReqPlnFlag <> 'I' AND Source <> 'E' Group BY StockCode " & _ " ) a " & _ "LEFT JOIN BomStructure b on a.StockCode = b.ParentPart " & _ "LEFT JOIN ( " & _ " select StockCode, sum(QtyOnHand) FQOH, Sum(QtyAllocated) FQOO, Sum(QtyInTransit) FQIT, Sum(QtyOnOrder) FQOA " & _ " from InvWarehouse " & _ " where Warehouse in ('01','DS','RM') " & _ " group by StockCode " & _ ") c on a.StockCode = c.StockCode " & _ "LEFT JOIN ( " & _ " select StockCode, sum(QtyOnHand) CQOH, Sum(QtyAllocated) CQOO, Sum(QtyInTransit) CQIT, Sum(QtyOnOrder) CQOA " & _ " from InvWarehouse " & _ " where Warehouse in ('01','DS','RM') " & _ " group by StockCode " & _ ") d on b.Component = d.StockCode " SQL = SQL & _ "LEFT JOIN InvMaster e on a.StockCode = e.StockCode " & _ "WHERE 1 = 1 " & _ "and e.Buyer in ('" & Range(arrBuyer(0)) & "','" & Range(arrBuyer(1)) & "','" & Range(arrBuyer(2)) & "','" & Range(arrBuyer(3)) & "') " & _ "ORDER BY a.StockCode "
DLookUp Function not match
Why when I write code like this the result doesn't match the table? Code: =DLookUp("Quiz","Grade_Fall_2022","Program_ID= '" & DLookUp("ID","Program","Program= '" & [Text_Program1] & "'") & "'" And "TIS_ID= " & [Text_ID]) Result: https://i.stack.imgur.com/c84Xh.png Source Table: https://i.stack.imgur.com/aywZE.png
Try with: =DLookUp("Quiz","Grade_Fall_2022","Program_ID = '" & DLookUp("ID","Program","Program = '" & [Text_Program1] & "'") & "' And TIS_ID = " & [Text_ID])
DateTime field not Updateable
Here's my code: strSQL = "UPDATE tblTransactions SET Verifier = '" & strUserName & "' WHERE PatientAccountNumber = '" & Me.PatientAccountNumber & "'" strSQL = "UPDATE tblTransactions SET VerifierAssgnDate = #" & Date & "# WHERE PatientAccountNumber = '" & Me.PatientAccountNumber & "'" "Verifier" updates fine, but "VerifierAssignDate" throws a "field not updateable" error. It's a DateTime field. Thanks!
You can and should combine these (and VerifierAssgnDate was probably misspelled): strSQL = "UPDATE tblTransactions " & _ "SET Verifier = '" & strUserName & "', VerifierAssignDate = Date() " & _ "WHERE PatientAccountNumber = '" & Me.PatientAccountNumber & "'"
VBA inserting Update Statement with multiple columns to table in SQL
can anyone help me where am I doing wrong.. im building an update statement with multiple columns to update MSQL with VBA and values from Excel. heres my simple code, Im parsing variable in function to execute SQL command (I have a table name "People" and I have define all the column) below: Function GetUpdateTextSQL(PIC As String, Customer As String, DOB As Date, Rank As String, Organization As String, Status As String, Gender As String, Religion As String, Hobby As String, CreatedBy1 As String, CreatedOn1 As Date, ChangedBy1 As String, ChangedOn1 As Date, PeopleID As Integer) As String SQLStr = _ "UPDATE People" & _ "SET PIC = " & _ "'" & PIC & "', Customer = '" & Customer & "'," & _ "DOB = '" & Format(DOB, "yyyy-mm-dd") & "', Rank = '" & Rank & "'," & _ "Organization = '" & Organization & "',Status = '" & Status & "', Gender = '" & Gender & "'," & _ "Religion = '" & Religion & "', Hobby = '" & Hobby & "'," & _ "CreatedBy = '" & CreatedBy1 & "', CreatedOn = '" & CreatedOn1 & "'," & _ "ChangedBy = '" & ChangedBy1 & "', ChangedOn = '" & ChangedOn1 & "'" & _ "WHERE PeopleID = & PeopleID &;" GetUpdateTextSQL = SQLStr //And here Im inserting and executing command below to get values from excel: For Each r In Range("A45", Range("A45").End(xlDown)) CmdForSave.CommandText = _ GetUpdateTextSQL( _ r.Offset(0, 1).Value, r.Offset(0, 2).Value, _ r.Offset(0, 3).Value, _ r.Offset(0, 4).Value, r.Offset(0, 5).Value, _ r.Offset(0, 6).Value, r.Offset(0, 7).Value, _ r.Offset(0, 8).Value, r.Offset(0, 9).Value, _ r.Offset(0, 10).Value, r.Offset(0, 11).Value, _ r.Offset(0, 12).Value, r.Offset(0, 13).Value, _ r.Offset(0, 0).Value) CmdForSave.Execute Next r The error I get is 'Incorrect Syntax near 'PIC' What is wrong with my code? Iam using MS SQL Express (SQL 2012)
Thanks All, I have figured it out why the problem is with space on the update SQL Statement. The right statement using VBA (space before SET, space before WHERE): SQLStr = _ "UPDATE People" & _ " SET PIC = '" & PIC & "', Customer = '" & Customer & "'" & _ " WHERE PeopleID = '" & PeopleID & "';"
Visual Studio Database custom criteria search wont work
I have created a database in Visual Studio and I am coding with VB.net I have created textboxes and checkboxes to match the fields that each will search when the search button is pressed . whenever i perform a search using the text boxes and checkbox i get an error. Item Name , Room , Broken, In Use, floor, are the fields searched by the tehe text in NameSearch, RoomSearch, BrokenSearchare, InUseSearch ,FloorSearchrespectivly ....etc this is thee code for the search button Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click RecordDataGridView.Refresh() Me.RecordBindingSource.Filter = "[Item Name]= '" & NameSearch.Text & "' And [Room]= '" & RoomSearch.Text & "' And [Make]= '" & MakeSearch.Text & _ "' And [Broken]= '" & BrokenSearch.CheckState & "' And [Replaced]= '" & ReplacedSearch.CheckState & "'And [ID#]= '" & IdentificationNumberSearch.Text & _ "' And [Floor]= '" & FloorSearch.Text & "' And [In Use]= '" & InUseSearch.CheckState & "'" Me.RecordTableAdapter.Fill(Me.MLGDatabaseDataSet.Record) RecordDataGridView.Refresh() End Sub the error for example I enter a text into item nameSearch and floorSearch and press search ,no result will be turned up as the other text boxes have no text in them.
Without addressing other issues, such as using a parameterized query to prevent SQL injections or using StringBuilder to more efficiently perform concatenation, I believe your issue may be a missing space in this snippet: ReplacedSearch.CheckState & "'And [ID#]= '" if you change this to ReplacedSearch.CheckState & "' And [ID#]= '" it may resolve the immediate error. However, you almost certainly have additional logic errors introduced by the OR statement in the middle (you probably want to surround the two clauses that are ORed with parentheses).
I spotted the same thing as #competent_tech. I would set it up this way to make it easier to debug. Dim strFilter As String = _ "[Item Name]= '" & NameSearch.Text & _ "' And [Room]= '" & RoomSearch.Text & _ "' And [Make]= '" & MakeSearch.Text & _ "' And [Broken]= '" & BrokenSearch.CheckState & _ "' Or [Replaced]= '" & ReplacedSearch.CheckState & _ "' And [ID#]= '" & IdentificationNumberSearch.Text & _ "' And [Floor]= '" & FloorSearch.Text & _ "' And [In Use]= '" & InUseSearch.CheckState & "'" Debug.Print(strFilter) RecordBindingSource.Filter = strFilter Edit: you want to filter only when there are conditions given 'filter string Dim strFilter As String = "" 'for check boxes you probably want to filter only if checked If BrokenSearch.CheckState = CheckState.Checked Then strFilter += "And [Broken]= " & BrokenSearch.CheckState & " " If ReplacedSearch.CheckState = CheckState.Checked Then strFilter += "And [Replaced]= " & ReplacedSearch.CheckState & " " If InUseSearch.CheckState = CheckState.Checked Then strFilter += "And [In Use]= " & InUseSearch.CheckState & " " 'for text boxes you want to filter only if has text If IdentificationNumberSearch.Text.Length > 0 Then strFilter += "And [ID#]= '" & IdentificationNumberSearch.Text & "' " If FloorSearch.Text.Length > 0 Then strFilter += "And [Floor]= " & FloorSearch.Text & " " If RoomSearch.Text.Length > 0 Then strFilter += "And [Room]= " & RoomSearch.Text & " " If MakeSearch.Text.Length > 0 Then strFilter += "And [Make]= '" & MakeSearch.Text & "' " If NameSearch.Text.Length > 0 Then strFilter += "And [Item Name]= '" & NameSearch.Text & "' " 'check to make sure there is at least one condition If strFilter.Length > 0 Then 'remove the first "And" strFilter = strFilter.Remove(0, 4) 'output Debug.Print(strFilter) 'set to filter RecordBindingSource.Filter = strFilter End If Edit: Your error "Cannot perform '=' operation on System.Boolean and System.String" I think the problem is that the filter value has to match the field type. For strings do this: [Make] = 'stringValue' using single quotes; For integers do this: [Floor] = 24 without single quotes; For bit do this: [Broken] = 1 without single quotes; It looks like Broken, Replaced and In Use are Bit type in the database. And I'm guessing that Floor and Room are Integer.