Insert New Records Only Into Sql Table Using Vba
I have an Excel workbook with the below code - Sub Button1_Click() Dim conn As New ADODB.Connection Dim iRowNo As Integer Dim sFirstName, sLastName As String With
Solution 1:
Your SQL query isn't quite right - there is no THEN in a SQL IF.
Also, you don't need to do anything if it does exist, so just use if not exists.
"IF NOT EXISTS (SELECT 1 FROM dbo.Customers WHERE FirstName = '" & sFirstName & "' and LastName = '" & sLastName & "') " & _
"INSERT INTO dbo.Customers (FirstName, LastName) " & _
"VALUES ('" & sFirstName & "', '" & sLastName & "')"Solution 2:
You can use sql query like this:
IF Exists ( Select1from dbo.customers where Firstname ='" & sFirstName & "'and LastName ='" & sLastName & "'THENUpdate dbo.customers set--other columns where Firstname = '" & sFirstName & "' and LastName = '" & sLastName & "'ELSEInsertinto dbo.Customers (FirstName, LastName) " & _
"values ('" & sFirstName & "', '" & sLastName & "')"
Not sure about the syntax for excel and C#, you can correct that similar to this query
Post a Comment for "Insert New Records Only Into Sql Table Using Vba"