Querying ODBC databases via SQL Server

JDias 136 Reputation points
2021-02-25T17:44:07.35+00:00

I want to make selects, updates, inserts, etc in a database from a linked server in MS SQL Server.

I connected the linked server [SQLITE3] to a sqlite3 database with SQLite3 ODBC Driver (www.ch-werner.de/sqliteodbc).

I accomplished to run selects with:

SELECT * FROM OPENQUERY([SQLITE3],'select * from log');

This returned the desired results.

However, doing a update/delete/insert like this returns an error, e.g.:

DELETE OPENQUERY([SQLITE3],'select * from log');

Msg 7330, Level 16, State 2, Line 4
Cannot fetch a row from OLE DB provider "MSDASQL" for linked server "SQLITE3".

How may I do writting operations?
May I use selects with a syntax like the following?

SELECT * FROM [SQLITE3].log -- gives error: non-existing object

Developer technologies Transact-SQL
SQL Server Other
{count} votes

3 answers

Sort by: Most helpful
  1. Sam of Simple Samples 5,546 Reputation points
    2021-02-25T18:19:02.48+00:00
    0 comments No comments

  2. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-02-25T22:33:05.11+00:00

    You could try

    DELETE SQLLITE3...log
    

    Note that you need three dots, SQLLITE3 is the server component.

    Using linked servers can often be a bumpy ride. Error message are often cryptic, as there are several layers. The error message you had should be acompanied with an informational message that comes the from OLE DB provider. And if you are lucky, it origins from the ODBC driver. But it can still be cryptic.

    0 comments No comments

  3. MelissaMa-MSFT 24,221 Reputation points
    2021-02-26T06:04:31.467+00:00

    Hi @JDias ,

    Welcome to Microsoft Q&A!

    You could try to define all column names in the log table, add where condition or use direct delete from statement as below examples:

    DELETE FROM OPENQUERY([SQLITE3],'select * from log');  
      
    DELETE FROM OPENQUERY([SQLITE3],'select COL1,COL2 from log');  
      
    DELETE FROM OPENQUERY([SQLITE3],'select COL1,COL2 from log where id=1');  
    
    DELETE OPENQUERY([SQLITE3], 'select * from log') where id=1  
      
    DELETE FROM [SQLITE3].[DATABASENAME].[SCHEMA].[log]  
    

    You could also try with below method mentioned in this forum which may be helpful.

    1. In the ODBC Data Source Administrator panel on the server, configure your linked server from System DSN.
    2. Under the Performance tab, uncheck the box 'Enable pre-fetch of data for queries'. OK, OK. Delete and re-create your linked server in SQL Management Studio.

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.