We have been using MSSQL and Access up to this point. We have one application that once all the data has been imported then goes thru MANY update queries. This particular application requires an embedded database. Easy with the Inner Joins using MS Access.
We are switching to SQLite and obviously very new to SQLite.
Can't seem to get our Update Queries correct and/or efficient in SQLite. They are either taking forever (actually never finishing) or not working.
One example below is a many to one relationship. Trying to update one field in the "Many" table to a value in the "One" table based on a field that exist in both tables.
Table 1 (the one table)
Field (ID) - this field is unique
Field (Manager)
Table 2 (the many table)
Field (ID) - the values in this field are not unique and they link to the (ID) field in Table 1
Field (Contact)
Here is our query:
UPDATE [Table 2] SET [Contact] = (SELECT [Manager] FROM [Table 1]
WHERE [Table 2].[ID] = [Table 1].[ID])
We've tried this within SQLite Expert Professional, and the query never stops running. There are only 53,391 records in Table 2 to be updated. We have to shut down the SQLite Expert application every time. It just hangs.
We put this into our Test Code captured within a Transaction as follows:
Using cmd as new SQLiteCommand(cnn)
Using transaction = cnn.BeginTransaction
cmd.Transaction = transaction
cmd.CommandText = "UPDATE [Table 2] SET [Contact] = (SELECT [Manager] FROM [Table 1] WHERE [Table 2].[ID] = [Table 1].[ID])"
icount = cmd.ExecuteNonQuery
transaction.Commit()
End Using
End Using
This just never ends.
This worked:
UPDATE [Table 2] SET [Contact] = (SELECT [Manager] FROM [Table 1]
WHERE [Table 2].[ID] = [Table 1].[ID])
WHERE EXISTS (Select [Manager] FROM [Table 1]
WHERE [Table 2].[ID] = [Table 1].[ID])
not sure if there is a better or more faster approach? Not sure we understand why the WHERE EXISTS is required either.
We have another Update query issue trying to update a field within a table based on another field in that table. Kinds recursive.