About Author

Ritesh Shah is an author of many technical article on Microsoft Technology, especially on SQL-Server, .NET technology like C# and ASP.Net etc. He is having 10+ year of experience in IT industry and working as Consultant Project Leader and Database Administrator.

This is really very simple error regarding the setting ofthe provider you have used in your linked server. In our case, we have used “Microsoft.ACE.OLEDB.12.0” so we have to set theproperty of the same.

Said both of the techniques (OpenRowSetand OpenQuery) are almost same and may confuse many developers. I have beenasked many time personally by few of my team member in recent years as well asI have seen the same question in few forums like Experts-Exchange and Forums.Asp.Net.

Basically both commands requires to access remote data fromother data source like other SQL Server or may be Oracle or may be Access orExcel or anything else. Basically both of these commands requires connectioninformation of source database, table information etc.

OpenQuery gets all these information from LinkedServer where as in OpenRowSet,you have to provide all these information at run time. There is no otherdifference.

Let us see how does it works, we are going to create oneaccess file named “acc1.accdb” and will create one Employee table inside “acc1.accdb”file which will be stored in D drive of computer. You can have your own accessfile at your desired location.

Here is the query for the same:

–add link server

execsp_addlinkedserver

@server=‘LinkToAceess’,

@srvproduct=‘AccessDatabase’,

–if you haveolder version of access then kindly use old jet provider

–Microsoft.Jet.OLEDB.4.0

@provider=‘Microsoft.ACE.OLEDB.12.0’,

@datasrc=‘D:\db1.accdb’

GO

–add MDB’scredental, if any

EXECsp_addlinkedsrvlogin‘LinkToAceess’,‘false’

execsp_serveroption[LinkToAceess],‘Data Access’,‘true’

–check whether’LinkToAccess’ has been added

select*fromsys.servers

–list alltables available in Testing.MDB

execsp_tables_ex‘LinkToAceess’

–now query withOPENSOURCE

SELECT*FROMOPENQUERY(LinkToAceess,‘SELECT id,empname FROMemployee’)

–now query withOPENROWSET,

–which will notuse our linked server

SELECT*

FROMOPENROWSET(‘Microsoft.ACE.OLEDB.12.0’,

‘D:\db1.accdb’;

”;”,Employee);

–if you getfollowing error

–Msg 15281,Level 16, State 1, Line 1

–SQL Serverblocked access to STATEMENT ‘OpenRowset/OpenDatasource’ of component ‘Ad HocDistributed Queries’ because this component is turned off as part of thesecurity configuration for this server. A system administrator can enable theuse of ‘Ad Hoc Distributed Queries’ by using sp_configure. For more informationabout enabling ‘Ad Hoc Distributed Queries’, see “Surface AreaConfiguration” in SQL Server Books Online.

Said both of the techniques (OpenRowSetand OpenQuery) are almost same and may confuse many developers. I have beenasked many time personally by few of my team member in recent years as well asI have seen the same question in few forums like Experts-Exchange and Forums.Asp.Net.

Basically both commands requires to access remote data fromother data source like other SQL Server or may be Oracle or may be Access orExcel or anything else. Basically both of these commands requires connectioninformation of source database, table information etc.

OpenQuery gets all these information from LinkedServer where as in OpenRowSet,you have to provide all these information at run time. There is no otherdifference.

Let us see how does it works, we are going to create oneaccess file named “acc1.accdb” and will create one Employee table inside “acc1.accdb”file which will be stored in D drive of computer. You can have your own accessfile at your desired location.

Here is the query for the same:

–add link server

execsp_addlinkedserver

@server=‘LinkToAceess’,

@srvproduct=‘AccessDatabase’,

–if you haveolder version of access then kindly use old jet provider

–Microsoft.Jet.OLEDB.4.0

@provider=‘Microsoft.ACE.OLEDB.12.0’,

@datasrc=‘D:\db1.accdb’

GO

–add MDB’scredental, if any

EXECsp_addlinkedsrvlogin‘LinkToAceess’,‘false’

execsp_serveroption[LinkToAceess],‘Data Access’,‘true’

–check whether’LinkToAccess’ has been added

select*fromsys.servers

–list alltables available in Testing.MDB

execsp_tables_ex‘LinkToAceess’

–now query withOPENSOURCE

SELECT*FROMOPENQUERY(LinkToAceess,‘SELECT id,empname FROMemployee’)

–now query withOPENROWSET,

–which will notuse our linked server

SELECT*

FROMOPENROWSET(‘Microsoft.ACE.OLEDB.12.0’,

‘D:\db1.accdb’;

”;”,Employee);

–if you getfollowing error

–Msg 15281,Level 16, State 1, Line 1

–SQL Serverblocked access to STATEMENT ‘OpenRowset/OpenDatasource’ of component ‘Ad HocDistributed Queries’ because this component is turned off as part of thesecurity configuration for this server. A system administrator can enable theuse of ‘Ad Hoc Distributed Queries’ by using sp_configure. For more informationabout enabling ‘Ad Hoc Distributed Queries’, see “Surface AreaConfiguration” in SQL Server Books Online.

Installing MySQL Server in CentOS 7 As a part of some latest R&D, I wanted to replicate some data from SQL Server to MySQL. Since there is no native way to do this, I am preparing my own framework which suits my requirement. To work on the project, I have purchased, recently, cloud space with […] The post Installing MySQL Server in CentOS 7 appeared firs […]

I have started working with Microsoft SQL Server 7.0 in late 1999 and even today I become very excited with the new version of Microsoft SQL Server. SQL Server 2016 has been in market for quite some time but now finally Microsoft has released service pack 1 for SQL Server 2016 to download. This […] The post SQL Server 2016 Service Pack 1 appeared first on […]

SQL Server 2014 CTP 1 available to download SQL Server 2014 is recently announced in TechED Europe. I am really very excited to download and knew more about it. You can download SQL Server 2014 CTP 1 from here. Each new version of software needs compatible hardware and software. Those are mandatory before you try […] The post SQL Server 2014 CTP 1 available […]

I have very often seen that people are so used to with traditional methods and hence never think in terms of set based programming, they used to write code with evergreen traditional method which people used to use in sequential language like C or many be ASP etc. Let us see this practically with an […] The post Dynamic filter in WHERE Clause with CASE_WHEN_ […]

SQL Server Login permission to read queue message count of Service Broker Few weeks back, I have written one article “Find Service Broker Queue Count in SQL Server”. One of the blog reader has created the UDF I have given in that article and tried to fetch value of that UDF from his web application […] The post SQL Server Login permission to read queue messa […]

Count active Thread for Service Broker Queue in SQL Server Since we have big service broker setup, it is very important to know for us about thread running for target queue. Target queue is the queue which suppose to do the work of actual business logic and we used to give proper naming convention to […] The post Count active Thread for Service Broker Queue […]

Error Fix – Replication subscriber does not exist on the server anymore Recently one of my client has faced weird situation related to SQL Server Replication. Their main database goes down so they started running mirroring database as a primary database, removed subscriber and distributor. Once they have setup primary server back properly, they have […] The […]

Bug Fix – Show Result Pane not working in SQL Server 2012 with CTRL + R Once you are addicted with some keyboard short keys then you hate to use mouse for those things for sure. SQL Developer/DBA are, mostly, addicted with “CTRL + R” short key which is being used to show/hide result panel […] The post Bug Fix – Show Result Pane not working in SQL Server 2012 […]

Find disabled job in SQL Server I, generally, don’t tend to keep disabled job in my server. I would always like to remove disabled job after generating the script and keep the backup of script and this is the reason whenever someone intentionally or unintentionally disabled the job, my monitoring application shows the server name […] The post Find disabled j […]

Copy and archive SQL Server database backup Though you are working as SQL Server DBA, you have to think, at least sometime, beyond relational. We can schedule different types of backup, ie: full, differential, transaction etc., from within SQL Server but what if you want to set archiving policy also? There are so many different […] The post Copy and archive […]

Error Fix Database Mail XPs error in SQL Server Today I have received one email from one of the blog reader since he was facing an issue in his .NET application which is related to SMTP email in SQL Server. Here is the error he was facing: SQL Server blocked access to procedure ‘dbo.sp_send_dbmail’ of […] The post Error Fix Database Mail XPs error in SQL Ser […]

Deal with message queue conversation with state CD in Service Broker Each side of a Service Broker conversation is represented by a conversation endpoint and sys.conversation_endpoints catalog view contains a row per conversation endpoint in the database. Generally when service broker accept record in message queue, it used to process message and remove it a […]

Keep watch on Service Broker Queue Delay in SQL Server We have very large setup of Service Broker enabled in our live databases. Out of which, some of the nightly business processes create a huge target queue which we suppose to finish during the night time itself. If we can’t cleanup all these queue in […] The post Keep watch on Service Broker Queue Delay i […]

Milestone of 500+ article This post is 504th post in my blog. I have started sharing my knowledge in form of blog article three years back. Initially I, just, wanted to keep my script collection online so that I can find it easily as and when I need it but later on my blog has […] The post Milestone of 500+ article appeared first on Ritesh Shah (Extreme-Advi […]

Update XML attribute value based on condition with XQUERY Recently I had one article which shows how to update XML attribute value which doesn’t had any checking/condition while update. One of the reader asked me how can he update value based on condition. Here is the solution for it. INSERT INTO Orders (ClientID,TestDetails) SELECT ‘CHEM02′,'

Query to Find missing job in SQL Server While managing multiple instance of SQL Server, it is little bit difficult to keep watch on every jobs running in every SQL Server Instances we are managing. I have created one monitoring application which suppose to be watched by at least one DBA 24*7. I have one […] The post Query to Find missing job in SQL Server ap […]

Update XML attribute value with XQUERY in SQL Server Recently I have written article on Inserting attribute in existing XML Elements and Delete XML element. Today I am going to show how to update existing attribute value in XML elements. INSERT INTO Orders (ClientID,TestDetails) SELECT ‘CHEM02′,'’ UNION ALL SELECT ‘ACCU01′,'

XQuery in SQL Server to insert XML attribute in existing XML element Use of XML is being popular in SQL Server and it comes handy in many situation as I have explained in many of my past Xquery/XML related article. Today I come up with one more example which shows usage of XQuery to insert […] The post XQuery in SQL Server to insert XML attribute in existing […]

XQuery in SQL Server to delete XML element Use of XML is being popular in SQL Server and it comes handy in many situation as I have explained in many of my past Xquery/XML related article. Today I come up with one more example which shows usage of XQuery to delete element from XML data […] The post XQuery in SQL Server to delete XML element appeared first on […]

WHERE clause on XML column in SQL Server table Xquery is one of the wonderful feature I appreciate most in SQL Server. Due to lack of knowledge, people doesn’t tend to use it. I always prefer XML in case of I have to pass multiple values in one parameter of stored procedure. It is even […] The post WHERE clause on XML column in SQL Server table appeared firs […]