Monday , December 23 2024

How To Use OPENROWSET in SQL Server

With OPENROWSET, you can access remote data using OLE DB. OPENROWSET is a system function as you will see below.

With this method, we provide one-time access to data by establishing an AD HOC connection. If one-time access to the data is required, it can be used instead of the linked server. If you need to access the data frequently, you can choose the linked server. For more detailed information about Linked Server, you can write “Linked Server” to the Seach section of our website.

You can use OPENROWSET as a one-time alternative to the linked server as follows. You must type the instance name that you want to access instead of DestinationInstanceName.

If “Ad Hoc Distributed Queries” is turned off on the instance you execute the above query, you will get the error as follows.

Msg 15281, Level 16, State 1, Line 1

SQL Server blocked access to STATEMENT ‘OpenRowset/OpenDatasource’ of component ‘Ad Hoc Distributed Queries’

because this component is turned off as part of the security configuration for this server.

A system administrator can enable the use of ‘Ad Hoc Distributed Queries’ by using sp_configure.

For more information about enabling ‘Ad Hoc Distributed Queries’, search for ‘Ad Hoc Distributed Queries’ in SQL Server Books Online.

To avoid this error, you must execute the following script on the instance where you execute the above query.

If your target server is Access database, your query should be as follows.

You can upload your files to the database using the OPENROWSET with the BULK option. You can find the details in the article “How To Import Images To SQL Server with TSQL Codes“.

You can join the data fetched from OPENROWSET with the data in your table and insert this result set to another table.

Loading

About dbtut

We are a team with over 10 years of database management and BI experience. Our Expertises: Oracle, SQL Server, PostgreSQL, MySQL, MongoDB, Elasticsearch, Kibana, Grafana.

Leave a Reply

Your email address will not be published. Required fields are marked *