mweidner
asked on
SQL VBScript check database first before appending new record
I need the SQL in this script to check before appending the database, if exist a record with the same date in field F1 (which is the date field) AND is = to F2 to quit instead of adding another record with the same data.
SQL would think like this - Hey, this guy has a habit of clicking on this script several times a day and he gets duplicate entries, so lets check the database against the .csv file first, we may not have to do anything. Along with F1 the date we will check F2 because F2 is never the same. If we find a identical record we will terminate without even informing him. If all is ok we will append the database with a new record.
csv file
10/13/2007,4239,4195,8075, 36.13,0.00 ,74.46,206 7,2046,102 47,21.55,0 .00,73.99, 0
script
Option Explicit
Dim oJet 'As DAO.DBEngine
Dim oDB 'As DAO.Database
Dim oTDef 'As DAO.TableDef
Dim blTExists 'As Boolean
Dim strSQL 'As String
Const DB_NAME = "C:\rubyfiles\tanks\gasdat a.mdb"
Const TBL_NAME = "Reg_VR_data"
Const DATA_SOURCE = "[Text;HDR=NO;FMT=Delimite d;Database =C:\rubyfi les\tanks\ ;].regular _data.csv"
Set oJet = CreateObject("DAO.DBEngine .36")
Set oDB = oJet.OpenDatabase(DB_NAME)
For Each oTDef In oDB.TableDefs
If oTDef.Name = TBL_NAME Then
blTExists = True
Exit For
End If
Next
If blTExists Then
strSQL = "INSERT INTO " & TBL_NAME _
& " SELECT * FROM " & DATA_SOURCE & ";"
Else
strSQL = "SELECT * INTO " & TBL_NAME _
& " FROM " & DATA_SOURCE & ";"
End If
oDB.Execute strSQL
oDB.Close
SQL would think like this - Hey, this guy has a habit of clicking on this script several times a day and he gets duplicate entries, so lets check the database against the .csv file first, we may not have to do anything. Along with F1 the date we will check F2 because F2 is never the same. If we find a identical record we will terminate without even informing him. If all is ok we will append the database with a new record.
csv file
10/13/2007,4239,4195,8075,
script
Option Explicit
Dim oJet 'As DAO.DBEngine
Dim oDB 'As DAO.Database
Dim oTDef 'As DAO.TableDef
Dim blTExists 'As Boolean
Dim strSQL 'As String
Const DB_NAME = "C:\rubyfiles\tanks\gasdat
Const TBL_NAME = "Reg_VR_data"
Const DATA_SOURCE = "[Text;HDR=NO;FMT=Delimite
Set oJet = CreateObject("DAO.DBEngine
Set oDB = oJet.OpenDatabase(DB_NAME)
For Each oTDef In oDB.TableDefs
If oTDef.Name = TBL_NAME Then
blTExists = True
Exit For
End If
Next
If blTExists Then
strSQL = "INSERT INTO " & TBL_NAME _
& " SELECT * FROM " & DATA_SOURCE & ";"
Else
strSQL = "SELECT * INTO " & TBL_NAME _
& " FROM " & DATA_SOURCE & ";"
End If
oDB.Execute strSQL
oDB.Close
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Good choice.
ASKER