I am trying to insert from two MS Access tables into one ms access table.
In one of the two tables there is a column that contains a duplicate value.
What I have at the moment is:
Dim MySQL As String = "INSERT INTO XML_TEST_CASE (" & _
"TCParmId, " & _
"TestCase, " & _
"MessageType, " & _
"FileNo, " & _
"InstructionNo, " & _
"TransactionNo, " & _
"ElementNo, " & _
"MessageSection, " & _
"ElementLevel, " & _
"FullElementPath, " & _
"ElementValue, " & _
"ElementValueNew, " & _
"NameSpace, " & _
"NameSpaceValue, " & _
"Attribute, " & _
"AttributeValue, " & _
"TestCaseDescription) " & _
"SELECT " & _
"P.TCParmId, " & _
"P.TestCase, " & _
"P.MessageType, " & _
"P.FileNo, " & _
"P.InstructionNo, " & _
"P.TransactionNo, " & _
"S.ElementNo, " & _
"S.MessageSection, " & _
"S.ElementLevel, " & _
"S.ElementPath + S.Element, " & _
"S.ElementValue, " & _
"S.ElementValue, " & _
"S.NameSpace, " & _
"S.NameSpaceValue, " & _
"S.Attribute, " & _
"S.AttributeValue, " & _
"P.TestCaseDescription " & _
"FROM XML_TEST_CASE_PARAMETER P, XML_MESSAGE_STRUCTURE S " & _
"WHERE S.MessageType = P.MessageType " & _
"AND P.TestCase = '" & MyTestCase & "' " & _
"AND P.MessageType = '" & MyMessageType & "' " & _
"AND P.FileNo = " & MyFileNo & " " & _
"AND P.InstructionNo = " & MyInstructionNo & " " & _
"AND P.TransactionNo = " & MyTransactionNo & ";"
The problem is that in the XML_MESSAGE_STRUCTURE table it happens that S.ElementNo sometimes appreas more than once which then cuasing that I get also more than one records in my destination table for the specific ElementNo. This I do not want.
Any help is appreaciated.
Rico
Instead of SELECT DISTINCT, you could group by all of the fields except ElementNo. For ElementNo, you would select Max(ElementNo):
Dim MySQL As String = "INSERT INTO XML_TEST_CASE (" & _
"TCParmId, " & _
"TestCase, " & _
"MessageType, " & _
"FileNo, " & _
"InstructionNo, " & _
"TransactionNo, " & _
"ElementNo, " & _
"MessageSection, " & _
...
"SELECT " & _
"P.TCParmId, " & _
"P.TestCase, " & _
"P.MessageType, " & _
"P.FileNo, " & _
"P.InstructionNo, " & _
"P.TransactionNo, " & _
"Max(S.ElementNo), " & _
"S.MessageSection, " & _
...
"FROM XML_TEST_CASE_PARAMETER P, XML_MESSAGE_STRUCTURE S " & _
"WHERE S.MessageType = P.MessageType " & _
"AND P.TestCase = '" & MyTestCase & "' " & _
"AND P.MessageType = '" & MyMessageType & "' " & _
"AND P.FileNo = " & MyFileNo & " " & _
"AND P.InstructionNo = " & MyInstructionNo & " " & _
"AND P.TransactionNo = " & MyTransactionNo & _
"GROUP BY " & _
"P.TCParmId, " & _
"P.TestCase, " & _
"P.MessageType, " & _
"P.FileNo, " & _
"P.InstructionNo, " & _
"P.TransactionNo, " & _
"S.MessageSection, " & _
...
";"
Related
I have the following code using the ConcatRelated function. the current sql works with 1 where condition but I can't figure out how to get the sql to work with 2 conditions. It works when use it in a query. I get issues and the code does not debug.
ItemsShpdInvSQL = "SELECT InvoiceAllShippedItems.ShipmentID, InvoiceAllShippedItems.ShipType, " _
& "InvoiceAllShippedItems.BillTo, InvoiceAllShippedItems.CustID AS Consignee, " _
& "InvoiceAllShippedItems.PayMethod, InvoiceAllShippedItems.PaidInFull, " _
& "First(InvoiceAllShippedItems.Units) AS Units, " _
& "'Freight Charges - Ref. WR: ' & (ConcatRelated('[WR]'," _
& "'[InvoiceAllShippedItems]','[BillTo] =' & [BillTo])) & " _
& "' - (' & Sum([Chargeable]) & ' ' & [Units] & ' " _
& ") - Number of Pieces: ' & [NoPieces] AS InvDetails, " _
& "Sum(InvoiceAllShippedItems.[# of Pieces]) AS NoPieces, " _
& "Sum(InvoiceAllShippedItems.Chargeable) AS Charge, " _
& "CustRate([custid],[ShipType]) AS CustomerRate, " _
& "IIf(([Charge])<6.1,20/[Charge],[CustomerRate]) AS RateMin " _
& "FROM InvoiceAllShippedItems " _
& "GROUP BY InvoiceAllShippedItems.ShipmentID, " _
& "InvoiceAllShippedItems.ShipType, InvoiceAllShippedItems.BillTo, " _
& "InvoiceAllShippedItems.CustID, InvoiceAllShippedItems.PayMethod, " _
& "InvoiceAllShippedItems.PaidInFull, InvoiceAllShippedItems.Units, " _
& "CustRate([custid],[ShipType]) " _
& "ORDER BY InvoiceAllShippedItems.BillTo;"
'& "CustRate([custid],[ShipType]);"
with this it works in a query but not in the code above:
'concatrelated("[WR]","InvoiceAllShippedItems","[BillTo]&[PayMethod] ='" & [BillTo] & [PayMethod] & "'")
I'm trying to add to the following query:
strSQL = "SELECT fldName, blkName, CDbl(fldValue) " & _
"FROM dbSecurities2 as S " & _
"WHERE " & _
"S.isin='" & Code & "' " & _
"AND " & _
"S.fldName='" & fldName & "' "
A row that makes the sum of the fldValue like:
strSQL = "SELECT fldName, blkName, CDbl(fldValue) " & _
"FROM dbSecurities2 as S " & _
"UNION " & _
"SELECT Sum(fldValue) AS fldValue " & _
"WHERE " & _
"S.isin='" & Code & "' " & _
"AND " & _
"S.fldName='" & fldName & "' "
the error is:
Run -time error '3141'. The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect
I found this is working:
strSQL = "SELECT fldName, blkName, CDbl(fldValue) " & _
"FROM dbSecurities2 as S " & _
"WHERE " & _
"S.isin='" & Code & "' " & _
"AND " & _
"S.fldName='" & fldName & "' " & _
"UNION " & _
"SELECT '' AS fldName, 'Total' AS Total, Sum(CDbl(fldValue)) " & _
"FROM dbSecurities2 AS B " & _
"WHERE " & _
"B.isin='" & Code & "' " & _
"AND " & _
"B.fldName='" & fldName & "' "
This should run as expected:
strSQL = "SELECT fldName, blkName, CDbl(fldValue) " & _
"FROM dbSecurities2 AS S " & _
"WHERE " & _
"S.isin='" & Code & "' " & _
"AND " & _
"S.fldName='" & fldName & "' " & _
"UNION ALL " & _
"SELECT TOP 1 "", "Total", Sum(CDbl(fldValue)) " & _
"FROM dbSecurities2"
If you have Null values, use Nz:
strSQL = "SELECT fldName, blkName, CDbl(Nz(fldValue, 0)) " & _
"FROM dbSecurities2 AS S " & _
"WHERE " & _
"S.isin='" & Code & "' " & _
"AND " & _
"S.fldName='" & fldName & "' " & _
"UNION ALL " & _
"SELECT TOP 1 "", "Total", Sum(CDbl(Nz(fldValue, 0))) " & _
"FROM dbSecurities2"
The code isn't working properly, getting a Syntax error in INSERT INTO statement error. Trying to insert data with an inner join from an MS Access form where the user types in responses and selects a button to add the detail to a new table. Based on the information provided I want it to insert the data into another table where it is joined by a table to get the fiscal data.
stSQL = "INSERT INTO PO_Information " & _
"(Partner, PO_Number, PO_Title, Cost_Center, Description, Line_1_Amt, Line_2_Amt, Date_Added, Month_Added, Year_Added, FY, Qtr, FY_Qtr, FW, FWeek)" & _
"Select New_PO_Information.Partner, " & _
"New_PO_Information.PO_Number, " & _
"New_PO_Information.PO_Title, " & _
"New_PO_Information.Cost_Center, " & _
"New_PO_Information.Description, " & _
"New_PO_Information.Line_1_Amt, " & _
"New_PO_Information.Line_2_Amt, " & _
"New_PO_Information.Date_Added, " & _
"New_PO_Information.Month_Added, " & _
"New_PO_Information.Year_Added, " & _
"Fiscal_Calendar.FY, " & _
"Fiscal_Calendar.Qtr, " & _
"Fiscal_Calendar.FY_Qtr, " & _
"Fiscal_Calendar.FW, " & _
"Fiscal_Calendar.FWeek, " & _
"From New_PO_Information INNER JOIN Fiscal_Calendar " & _
"ON New_PO_Information.Date_Added = Fiscal_Calendar.Calendar"
stSQL = stSQL & _
"From New_PO_Information " & _
"Where ((New_PO_Information.Partner)<>'') " & _
"AND ((New_PO_Information.PO_Number)<>'') " & _
"AND ((New_PO_Information.PO_Title)<>'') " & _
"AND ((New_PO_Information.Cost_Center)<>'') " & _
"AND ((New_PO_Information.Description)<>'');"
Can someone tell me where I went wrong and correct the code, please?
It's probably this comma here: "Fiscal_Calendar.FWeek, " & _
There shouldn't be a comma before the FROM statement. And make sure you only have one FROM statement:
stSQL = "INSERT INTO PO_Information " & _
"(Partner, PO_Number, PO_Title, Cost_Center, Description, Line_1_Amt, Line_2_Amt, Date_Added, Month_Added, Year_Added, FY, Qtr, FY_Qtr, FW, FWeek)" & _
"Select New_PO_Information.Partner, " & _
"New_PO_Information.PO_Number, " & _
"New_PO_Information.PO_Title, " & _
"New_PO_Information.Cost_Center, " & _
"New_PO_Information.Description, " & _
"New_PO_Information.Line_1_Amt, " & _
"New_PO_Information.Line_2_Amt, " & _
"New_PO_Information.Date_Added, " & _
"New_PO_Information.Month_Added, " & _
"New_PO_Information.Year_Added, " & _
"Fiscal_Calendar.FY, " & _
"Fiscal_Calendar.Qtr, " & _
"Fiscal_Calendar.FY_Qtr, " & _
"Fiscal_Calendar.FW, " & _
"Fiscal_Calendar.FWeek " & _
"FROM New_PO_Information INNER JOIN Fiscal_Calendar " & _
"ON New_PO_Information.Date_Added = Fiscal_Calendar.Calendar"
stSQL = stSQL & _
" Where ((New_PO_Information.Partner)<>'') " & _
"AND ((New_PO_Information.PO_Number)<>'') " & _
"AND ((New_PO_Information.PO_Title)<>'') " & _
"AND ((New_PO_Information.Cost_Center)<>'') " & _
"AND ((New_PO_Information.Description)<>'');"
If I created an append query, here is the code. Just trying to convert this into my SQL statement that I posted earlier.
INSERT INTO PO_Information ( Partner, PO_Number, PO_TItle, Cost_Center, Description, Line_1_Amt, Line_2_Amt, Date_Added, Month_Added, Year_Added, FY, Qtr, FY_Qtr, FW, FWeek )
SELECT New_PO_Information.Partner, New_PO_Information.PO_Number, New_PO_Information.PO_TItle, New_PO_Information.Cost_Center, New_PO_Information.Description, New_PO_Information.Line_1_Amt, New_PO_Information.Line_2_Amt, New_PO_Information.Date_Added, New_PO_Information.Month_Added, New_PO_Information.Year_Added, Fiscal_Calendar.FY, Fiscal_Calendar.Qtr, Fiscal_Calendar.FY_Qtr, Fiscal_Calendar.FW, Fiscal_Calendar.FWeek
FROM New_PO_Information INNER JOIN Fiscal_Calendar ON New_PO_Information.Date_Added = Fiscal_Calendar.Calendar;
I'm connecting to an AS/400 by an ODBC connection. I'm getting wacky errors when I try to insert and delete from the file. First, the format I've found for constructing a query stirng was like this:
"DELETE FROM <library>.<filename> WHERE <field> = <value>"
So, based on that, I wrote this:
"DELETE FROM RM#AVLIB.AV90909JWB WHERE MBR_NUM = " & mbr_num
It threw an error about not liking the DELETE, oddly enough, but I got it to go away if I removed the library altogether:
"DELETE FROM AV90909JWB WHERE MBR_NUM = " & mbr_num
I'm guessing it just didn't like the # symbol and worked without the library because my default library is the same one I'm using here. But now I'm getting an error in my INSERT that says:
ERROR [22003][Micro Focus][RUMBA Data Access][S1023934]Numeric value out of range. SQLCODE = -420
After running through all the values I'm passing, I didn't see any values that were longer than the field length so I'm not sure where this is coming from.
Here's my code as it sits now:
delQry = "DELETE FROM AV90301JWB WHERE MBR_CD = '" & MBR_CD & "' AND LOC_CD = '" & LOC_CD & "' AND PRP_ITM = '" & PRP_ITM & "'"
pushQry = "INSERT INTO AV90301JWB ( " & _
"MBR_NUM, " & _
"LOC_CD, " & _
"AVBLD_CLMT, " & _
"ADDRESS1, " & _
"ADDRESS2, " & _
"CITY, " & _
"STATE, " & _
"ZIPCODE, " & _
"AVBLD_DOS, " & _
"CNST_QLTY, " & _
"SEISMIC, " & _
"WIND, " & _
"AVBLD_DSC, " & _
"AVBLD_DSC1, " & _
"AVBLD_DSC2, " & _
"AVBLD_DSC3, " & _
"MISC_ADJ, " & _
"SEC_ID, " & _
"AVCOS_RC, " & _
"YR_BUILT, " & _
"NBR_STORY, " & _
"SQR_FT, " & _
"SUBCLASS, " & _
"OCC_CD1, " & _
"OCC_DSC1, " & _
"OCC_PCT1, " & _
"STORY_HT1, " & _
"OCC_CD2, " & _
"OCC_DSC2, " & _
"OCC_PCT2, " & _
"STORY_HT2, " & _
"OCC_CD3, " & _
"OCC_DSC3, " & _
"OCC_PCT3, " & _
"STORY_HT3, " & _
"OCC_CD4, " & _
"OCC_DSC4, " & _
"OCC_PCT4, " & _
"STORY_HT4, " & _
"OCC_CD5, " & _
"OCC_DSC5, " & _
"OCC_PCT5, " & _
"STORY_HT5, " & _
"HEAT_SYS, " & _
"COOL_SYS, " & _
"PWALL_EXT, " & _
"ROOF_MAT, " & _
"SPRINKLER, " & _
"MANL_FIRE, " & _
"AUTO_FIRE, " & _
"CNST_PCT1, " & _
"CNST_PCT2, " & _
"CNST_PCT3, " & _
"CNST_PCT4, " & _
"CNST_PCT5) " & _
"VALUES (" & _
"'" & MBR_NUM & "', " & _
"'" & LOC_CD & "', " & _
"'" & AVBLD_CLMT & "', " & _
"'" & ADDRESS1 & "', " & _
"'" & ADDRESS2 & "', " & _
"'" & CITY & "', " & _
"'" & STATE & "', " & _
"'" & ZIPCODE & "', " & _
"'" & AVBLD_DOS.ToShortDateString() & "', " & _
"'" & CNST_QLTY & "', " & _
"'" & SEISMIC & "', " & _
"'" & WIND & "', " & _
"'" & AVBLD_DSC & "', " & _
"'" & AVBLD_DSC1 & "', " & _
"'" & AVBLD_DSC2 & "', " & _
"'" & AVBLD_DSC3 & "', " & _
"'" & MISC_ADJ & "', " & _
"'" & SEC_ID & "', " & _
"" & Math.Round(AVCOS_RC, 2, MidpointRounding.AwayFromZero) & ", " & _
"'" & YR_BUILT & "', " & _
"'" & NBR_STORY & "', " & _
"'" & SQR_FT & "', " & _
"'" & SUBCLASS & "', " & _
"'" & OCC_CD1 & "', " & _
"'" & OCC_DSC1 & "', " & _
"'" & OCC_PCT1 & "', " & _
"'" & STORY_HT1 & "', " & _
"'" & OCC_CD2 & "', " & _
"'" & OCC_DSC2 & "', " & _
"'" & OCC_PCT2 & "', " & _
"'" & STORY_HT2 & "', " & _
"'" & OCC_CD3 & "', " & _
"'" & OCC_DSC3 & "', " & _
"'" & OCC_PCT3 & "', " & _
"'" & STORY_HT3 & "', " & _
"'" & OCC_CD4 & "', " & _
"'" & OCC_DSC4 & "', " & _
"'" & OCC_PCT4 & "', " & _
"'" & STORY_HT4 & "', " & _
"'" & OCC_CD5 & "', " & _
"'" & OCC_DSC5 & "', " & _
"'" & OCC_PCT5 & "', " & _
"'" & STORY_HT5 & "', " & _
"'" & HEAT_SYS & "', " & _
"'" & COOL_SYS & "', " & _
"'" & PWALL_EXT & "', " & _
"'" & ROOF_MAT & "', " & _
"'" & SPRINKLER & "', " & _
"'" & MANL_FIRE & "', " & _
"'" & AUTO_FIRE & "', " & _
"'" & CNST_PCT1 & "', " & _
"'" & CNST_PCT2 & "', " & _
"'" & CNST_PCT3 & "', " & _
"'" & CNST_PCT4 & "', " & _
"'" & CNST_PCT5 & "')"
Dim connectionString As String = ConfigurationManager.AppSettings("iSeriesConnString")
Dim insCommand As New OdbcCommand(pushQry)
Dim delCommand As New OdbcCommand(delQry)
Dim da As New OdbcDataAdapter
Using myConn As New OdbcConnection(connectionString)
insCommand.Connection = myConn
delCommand.Connection = myConn
myConn.Open()
da.InsertCommand = insCommand
da.DeleteCommand = delCommand
da.DeleteCommand.ExecuteNonQuery()
da.InsertCommand.ExecuteNonQuery()
End Using
Does anyone see something I'm missing or have any ideas about how to find my issue?
Thanks!
The standard ANSI SQL way of escaping invalid characters in an identifier or to escape an identifier which conflicts with a reserved keyword is to put it in double quotes:
DELETE FROM "RM#AVLIB".AV90909JWB WHERE ...
Obviously one of the values you are inserting or you are using in the where clause exceeds the range defined for that column. E.g. if a column has been defined as NUMERIC(2), you cannot insert 100.
Also I strongly urge you to use command parameters instead of string concatenation: See https://stackoverflow.com/a/2092851/880990
What I am trying to do is make a line of text appear every 50 strings read. I was trying to find a reaminder function to use on the GlobalVariables.TransferTracker interger, but I couldn't find anything online. Is there such a funciton? Or a differen't/better way to do this? If it helps here is my code:
Do While TransferRecord.Read()
'Start of writing to the SQL server.
SQLServerConnection.Open()
'SQL statement to transfer all the data that fits the requirements to the SQL server.
Dim SQLCommand1 As New SqlCommand("INSERT INTO dbo.b_Pulp_PI_Forte (" & _
"mill, " & _
"keyprinter_datetime, " & _
"bale_line_num, " & _
"pulp_line_id, " & _
"bale_id, " & _
"drop_datetime, " & _
"layboy_position, " & _
"bale_gross_weight, " & _
"gross_value_flag, " & _
"bale_airdry_pct, " & _
"airdry_value_flag, " & _
"sheets_per_bale, " & _
"grader_test_flag, " & _
"dropped_num, " & _
"created_by, " & _
"CreatedDateTime, " & _
"Who_did_it, " & _
"Last_change_datetime) " & _
"VALUES (" & _
"'850', " & _
"'" & ProdDate & "', " & _
"'" & BaleLineNum & "', " & _
"'" & BaleLine & "', " & _
"'" & BaleNumber & "', " & _
"'" & ProdDate & "', " & _
"'0', " & _
"'" & GrossWeight & "', " & _
"'" & GrossWeightFlag & "', " & _
"'" & AirDry & "', " & _
"'" & AirDryFlag & "', " & _
"'0', " & _
"'N', " & _
"'0', " & _
"'BaleTrac', " & _
"'" & Date.Now & "', " & _
"'BaleTrac', " & _
"'" & Date.Now & "')")
'If DisplayCode is checked this will be printed to the screen.
If ApplicationPropertiesWindow.DisplayCodechkbx.Checked = True Then
MainTextBox.AppendText(Environment.NewLine & SQLCommand1.CommandText)
GlobalVariables.DisplayCode = True
End If
'Executing the SQL statement.
SQLCommand1.Connection = SQLServerConnection
SQLCommand1.ExecuteNonQuery()
SQLServerConnection.Close()
GlobalVariables.TransferTracker = GlobalVariables.TransferTracker + 1
'This is where I would like to have the remainder function.
'Making message to show that program is still running.
If GlobalVariables.TransferTracker = 50 Then
MainTextBox.AppendText(Environment.NewLine & "50 records transferred.")
End If
Loop
Right now I just have it set up so it will fire at 50 records, because I couldn't find the function.
The remainder is an operator in VB, Mod:
If GlobalVariables.TransferTracker Mod 50 = 0 Then …
As a general advice, don’t write … = True in your conditions. Its redundancy is redundant.