我想在表中插入值,使用INSERT INTO
语句,但我想使用不同的分隔符(不是逗号),我该怎么做?
我不想使用逗号,原因如下:
数据采用以下格式:
|something|somethingelse|something3 ,moretextinsamefield|
field1 = "something"
field2 = "somethingelse"
field3 = "something3 ,something4"
答案 0 :(得分:4)
为什么不使用Split函数来指定分隔符:
Dim aValues As Variant
aValues = Split("|something|somethingelse|something3 ,something4", "|")
<强> ADDITION 强>
以下是VBA中的一些示例代码。
Public Sub AppendValues()
Const SOURCE_VALUES = "|something|somethingelse|something3 ,moretextinsamefield|"
Dim aValues As Variant
aValues = Split(SOURCE_VALUES, "|")
Dim oDB As DAO.Database
Dim oRS As DAO.Recordset
Set oDB = DBEngine.Workspaces(0).Databases(0)
Set oRS = oDB.OpenRecordset("Table1", dbOpenTable)
oRS.AddNew
oRS("Col1") = aValues(1)
oRS("Col2") = aValues(2)
oRS("Col3") = aValues(3)
oRS.Update
Set oRS = Nothing
Set oDB = Nothing
End Sub
答案 1 :(得分:2)
执行预插入操作并清理数据会更容易吗?将|
替换为,
,并将所有值括在引号中(或类似名称)?
如果没有,我认为如果您在文本文件中格式化数据,则可以使用Access导入文本并指定|
是分隔符,而不是,
。
答案 2 :(得分:2)
查看此文章:Pipe Delimited File into access database
试试这段代码。
方法如下:
- 将数据从文本文件导入临时表('Import')
- 通过加入导入来更新表中的现有记录('Dest') 表和现有表
- 从“导入”表中选择不存在的记录 在'Dest'表中
- 将这些新记录插入“Dest”
醇>您需要按照修改查询 你的桌子结构。
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:\VBNET\Access\bin\dest.mdb")
'Import data to a temporary table
Dim cmd As New OleDbCommand("SELECT * INTO [import] FROM [Text;FMT=Delimited;Database=G:\VBNET\Access\bin;Hdr=Yes].[Students.txt]", conn)
conn.Open()
cmd.ExecuteNonQuery()
'Update Existing records
Dim updateQuery As String = "UPDATE Dest INNER JOIN Import ON Dest.id = Import.F1 set Dest.Name = Import.F2"
Dim queryCmd As New OleDbCommand(updateQuery, conn)
queryCmd.ExecuteNonQuery()
Dim selectQuery = "select F1, F2, F3 from Import where F1 not in (select Id from Dest) "
queryCmd = New OleDbCommand(selectQuery, conn)
Dim dataReader As OleDbDataReader
dataReader = queryCmd.ExecuteReader()
Dim appendrecords As New ArrayList()
Dim insertQuery As String
While dataReader.Read()
Dim F1 As String = dataReader.GetString(0).ToString()
Dim F2 As String = dataReader.GetString(1).ToString()
Dim F3 As Integer = dataReader.GetInt32(2).ToString()
insertQuery = "insert into Dest values ('" & F1 & "', '" & F2 & "', " & F3 & ")"
appendrecords.Add(insertQuery)
End While
dataReader.Close()
Dim i As Integer
For i = 0 To appendrecords.Count - 1
Dim insertCmd As OleDbCommand = New OleDbCommand(appendrecords(i), conn)
insertCmd.ExecuteNonQuery()
Next
conn.Close()
答案 3 :(得分:2)
使用一堆嵌套的Mid()和InStr()函数编写SQL来实现这一目标会非常复杂。
相反,我会使用一个使用Split()的函数。
Public Function SplitField(varInput As Variant, strDelimiter As String, lngItemRequested As Long) As Variant
Dim varTemp As Variant
Dim arrInput() As String
varTemp = varInput
If Left(varTemp, 1) = strDelimiter Then
varTemp = Mid(varTemp, 2)
End If
If right(varTemp, 1) = strDelimiter Then
varTemp = Left(varTemp, Len(varTemp) - 1)
End If
arrInput = Split(varTemp, strDelimiter)
If lngItemRequested - 1 <= UBound(arrInput()) Then
SplitField = arrInput(lngItemRequested - 1)
If SplitField = vbNullString Then
SplitField = Null
End If
Else
SplitField = Null
End If
End Function
然后在SQL中,你这样称呼它:
INSERT INTO TargetTable( Field1, Field2, Field3, Field4 )
SELECT SourceTable.SourceField, SplitField([SourceField],"|",1),
SplitField([SourceField],"|",2),
SplitField([SourceField],"|",3),
SplitField([SourceField],"|",4)
FROM SourceTable
请注意,即使源字段中有可变数量的子部分,也可以使用我编写的函数。也就是说,如果有些部分有4个部分而且有部分有2个部分,那么无关紧要,因为函数对于不存在的部分返回Null。
答案 4 :(得分:1)
如果您希望使用INSERT语句执行此操作,则很简单。像这样调用子程序:
Call InsertValues("|something|somethingelse|something3 ,moretextinsamefield|")
取消注释Execute行以实际执行INSERT而不是仅显示语句。
Public Sub InsertValues(ByVal pstrInput As String)
Dim i As Integer
Dim strSql As String
Dim strValList As String
Dim varValues As Variant
varValues = Split(pstrInput, "|")
'first and last array members are empty strings; skip them '
For i = 1 To 3
strValList = strValList & ", " & Chr(34) & varValues(i) & Chr(34)
Next i
'strip off leading comma and space '
strValList = Mid(strValList, 3)
strSql = "INSERT INTO YourTable (field1, field2, field3)" & _
vbNewLine & "Values (" & strValList & ");"
Debug.Print strSql
'CurrentDb.Execute strSql, dbFailOnError '
End Sub