An error occurred while preparing the query

Hello I am trying to run a query via tsql against ad. Below is the error I am getting. I have read the http://msdn2.microsoft.com/en-US/library/ms190803.aspx and changed the domain but still having issues. Any help would be appreciated. EXEC sp_addlinkedserver 'ADSI', 'Active Directory Service Interfaces', 'ADSDSOObject', 'adsdatasource' GO SELECT * FROM OPENQUERY( ADSI, 'SELECT Name, SN, ST FROM ''LDAP://ADSISrv/ OU=Users,DC=XXXXX,DC=LOCAL'' WHERE objectCategory = ''Person'' AND objectClass = ''user''') Msg 7321, Level 16, State 2, Line 1 An error occurred while preparing the query "SELECT Name, SN, ST FROM 'LDAP://ADSISrv/ OU=Users,DC=XXXXX,DC=LOCAL' WHERE objectCategory = 'Person' AND objectClass = 'user'" for execution against OLE DB provider "ADSDSOObject" for linked server "ADSI".
  • Remove From My Forums
  • Question

  • Hello I am trying to run a query via tsql against ad. Below is the error I am getting. I have read the http://msdn2.microsoft.com/en-US/library/ms190803.aspx and changed the domain but still having issues. Any help would be appreciated.


    EXEC sp_addlinkedserver ‘ADSI’, ‘Active Directory Service Interfaces’,


    ‘ADSDSOObject’, ‘adsdatasource’

    GO


    SELECT *


    FROM OPENQUERY( ADSI,


    ‘SELECT Name, SN, ST

    FROM »LDAP://ADSISrv/ OU=Users,DC=XXXXX,DC=LOCAL»

    WHERE objectCategory = »Person» AND

    objectClass = »user»’)

    Msg 7321, Level 16, State 2, Line 1

    An error occurred while preparing the query «SELECT Name, SN, ST

    FROM ‘LDAP://ADSISrv/ OU=Users,DC=XXXXX,DC=LOCAL’

    WHERE objectCategory = ‘Person’ AND

    objectClass = ‘user'» for execution against OLE DB provider «ADSDSOObject» for linked server «ADSI».

Answers

  • I had the same issue like you, if you run in your personal computer is probably that you not experiment the same error but if you move to any other server that need the right permission to any user into the Active Directory.

    first execute your query:

    EXEC sp_addlinkedserver
    ‘ADSI’,
    ‘Active Directory Service Interfaces’,

    ‘ADSDSOObject’

    ,
    ‘adsdatasource’

    GO

    Like daw21 explain before follow the detail steps:

    Second step go to the SqlServer management Studio and edit manually the Linkserver name ADSI:

    go to Server Objects/Linked Servers/  and right click on ADSI  select the option Properties and then go to Security.

    Select the option :         Be made using this security context:

    and there fill the user  and Password about the valid user with permission into Active Directory, not forget before the domainuser.

    • Proposed as answer by

      Monday, August 8, 2011 9:18 PM

    • Marked as answer by
      Kalman Toth
      Tuesday, August 16, 2011 10:54 AM

  • Took me forever to get this working …

    First you have to make sure that Ad Hoc Distributed Queries is enabled …

    sp_configure 'Ad Hoc Distributed Queries', 1

    reconfigure

    Second you have to make sure that you specify a security context that has access to AD INCLUDING the DOMAIN

    Third you should put a row limit (e.g. SELECT TOP 100) in to avoid the issue where you hit the 1000 row limit so

    SELECT

    TOP 100
    *

    FROM

    OpenQuery(ADSI,
    'SELECT sAMAccountName FROM ''LDAP://DC=core,DC=acllab,DC=net'' WHERE objectCategory=''user'' ')

    • Marked as answer by
      Kalman Toth
      Tuesday, August 16, 2011 10:54 AM

Содержание

  1. An error occurred while preparing the query
  2. Asked by:
  3. Question
  4. All replies
  5. An error occurred while preparing the query
  6. Вопрос
  7. An error occurred while preparing the query
  8. Answered by:
  9. Question
  10. Answers
  11. All replies
  12. An error occurred while preparing the query
  13. Общие обсуждения
  14. An error occurred while preparing the query
  15. Вопрос
  16. Все ответы

An error occurred while preparing the query

This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions.

Asked by:

Question

Greetings of the Day.

I have a created a link server on SQL Server 2008 R2 databse of Service Now ODBC driver. And written a stored procedure using openquery to get the data from link server.

Now problem is that, when i have scheduled that stored procedure in sql jobs. When this job runs automatically then we are getting below error while when i start the job mannually then it’s not giving error.

An error occurred while preparing the query «. «

Please help me to resolve this issue.

Did you review the SQL Server job history for more details about the failed job? Please post the complete error message for further analysis.
Or you can try to recreate the job and scheduled the new job to run at a different time.

Regards,
Fanny Liu

Fanny Liu
TechNet Community Support

I had reviewed the job history. I am getting below message where as table is exist in the database.

Message
Executed as user: NT AUTHORITYSYSTEM. OLE DB provider «MSDASQL» for linked server «SNOWDSN» returned message «[DataDirect][ODBC OpenAccess SDK driver][OpenAccess SDK SQL Engine]Base table:incident not found.[10129]». [SQLSTATE 01000] (Message 7412) OLE DB provider «MSDASQL» for linked server «SNOWDSN» returned message «[DataDirect][ODBC OpenAccess SDK driver][OpenAccess SDK SQL Engine]Base table:sysapproval_approver not found.[10129]». [SQLSTATE 01000] (Message 7412) OLE DB provider «MSDASQL» for linked server «SNOWDSN» returned message «[DataDirect][ODBC OpenAccess SDK driver][OpenAccess SDK SQL Engine]Base table:problem not found.[10129]». [SQLSTATE 01000] (Message 7412). The step succeeded.

I am facing a similar problem , did u get the above error resolved ?

Источник

An error occurred while preparing the query

Вопрос

I have been trying to configure a linked server to AD and have found plenty of write ups on how to do it, but have had zero luck with getting it to work. I also read that the ability was removed after SQL 2008; is that correct? I am running 2014 and am seeing the following error trying to expand the tree:

TITLE: Microsoft SQL Server Management Studio
——————————

Failed to retrieve data for this request. (Microsoft.SqlServer.Management.Sdk.Sfc)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&LinkId=20476

An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)

Cannot obtain the required interface («IID_IDBSchemaRowset») from OLE DB provider «ADSDSOObject» for linked server «ADSI». (Microsoft SQL Server, Error: 7301)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&ProdVer=12.00.4100&EvtSrc=MSSQLServer&EvtID=7301&LinkId=20476

Then the following error when trying to query:

SELECT * FROM OpenQuery(ADSI, ‘SELECT displayName FROM »LDAP:// /DC=DOMAIN,DC= » WHERE objectCategory=»User» ‘)

Msg 7321, Level 16, State 2, Line 1
An error occurred while preparing the query «SELECT displayName FROM ‘LDAP://—-/DC=—-,DC=—-‘ WHERE objectCategory=’User’ » for execution against OLE DB provider «ADSDSOObject» for linked server «ADSI».

The linked server is set for ‘Be made by the login’s current security context’ and local to remote is configured SA to a domain account we use for lookups.

It does not seem to matter if I am logged in to the server with SQL or Windows credentials. Thank you in advance for any guidance!

Источник

An error occurred while preparing the query

This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions.

Answered by:

Question

Hello I am trying to run a query via tsql against ad. Below is the error I am getting. I have read the http://msdn2.microsoft.com/en-US/library/ms190803.aspx and changed the domain but still having issues. Any help would be appreciated.

EXEC sp_addlinkedserver ‘ADSI’ , ‘Active Directory Service Interfaces’ ,

FROM OPENQUERY ( ADSI ,

‘SELECT Name, SN, ST

FROM »LDAP://ADSISrv/ OU=Users,DC=XXXXX,DC=LOCAL»

WHERE objectCategory = »Person» AND

Msg 7321, Level 16, State 2, Line 1

An error occurred while preparing the query «SELECT Name, SN, ST

FROM ‘LDAP://ADSISrv/ OU=Users,DC=XXXXX,DC=LOCAL’

WHERE objectCategory = ‘Person’ AND

objectClass = ‘user’» for execution against OLE DB provider «ADSDSOObject» for linked server «ADSI».

Answers

I had the same issue like you, if you run in your personal computer is probably that you not experiment the same error but if you move to any other server that need the right permission to any user into the Active Directory.

first execute your query:

EXEC sp_addlinkedserver ‘ADSI’ , ‘Active Directory Service Interfaces’ ,

Like daw21 explain before follow the detail steps:

Second step go to the SqlServer management Studio and edit manually the Linkserver name ADSI:

go to Server Objects/Linked Servers/ and right click on ADSI select the option Properties and then go to Security.

Select the option : Be made using this security context:

and there fill the user and Password about the valid user with permission into Active Directory, not forget before the domainuser.

Took me forever to get this working .

First you have to make sure that Ad Hoc Distributed Queries is enabled .

OpenQuery ( ADSI , ‘SELECT sAMAccountName FROM »LDAP://DC=core,DC=acllab,DC=net» WHERE objectCategory=»user» ‘ )

I got it to work on the local database server. It didn’t seem to like the OU. Once I removed it the query completed. However I still get the message if I try and run the view from a remote computer using Mgmt Studio is there something I have to setup on the clients?

Источник

An error occurred while preparing the query

Общие обсуждения

При переносе приложений на новый SQL-сервер (9.0.3068) на нем понадобилось производить запросы к АД, используя ADsDSOObject. Проблема в том, что при запросе из оснастки запущенной на клиентской машине получаем ошибку:

SELECT * FROM OPENROWSET(‘ADSDSOObject’, »,
‘SELECT mail, displayName,givenname,distinguishedName, SAMAccountName, CN
FROM »LDAP:// DC = mydomain , DC =local » ‘)

Msg 7321, Level 16, State 2, Line 1
An error occurred while preparing the query «SELECT mail, displayName,givenname,distinguishedName, SAMAccountName, CN
FROM ‘LDAP:// DC=mydomain, DC=local’ » for execution against OLE DB provider «ADSDSOObject» for linked server «(null)».

При выполенении того же запроса из Microsoft SQL SMS запущенного непосредственно с сервера, запрос отрабатывает корректно.

Проверял на другом сервере SQL 2005 x64 9.0.3233 — та же самая ошибка.

На исходном сервере SQL 2005 9.0.3073 запрос отрабатывает.

Все сервера работают под доменной учетной записью. Авторизация mixed, в SQL SMS используется аутентификация Windows, права на всех серверах dbadmin.

Источник

An error occurred while preparing the query

Вопрос

Greetings of the Day.

I have a created a link server on SQL Server 2008 R2 databse of Service Now ODBC driver. And written a stored procedure using openquery to get the data from link server.

Now problem is that, when i have scheduled that stored procedure in sql jobs. When this job runs automatically then we are getting below error while when i start the job mannually then it’s not giving error.

An error occurred while preparing the query «. «

Please help me to resolve this issue.

Все ответы

Did you review the SQL Server job history for more details about the failed job? Please post the complete error message for further analysis.
Or you can try to recreate the job and scheduled the new job to run at a different time.

Regards,
Fanny Liu

Fanny Liu
TechNet Community Support

I had reviewed the job history. I am getting below message where as table is exist in the database.

Message
Executed as user: NT AUTHORITYSYSTEM. OLE DB provider «MSDASQL» for linked server «SNOWDSN» returned message «[DataDirect][ODBC OpenAccess SDK driver][OpenAccess SDK SQL Engine]Base table:incident not found.[10129]». [SQLSTATE 01000] (Message 7412) OLE DB provider «MSDASQL» for linked server «SNOWDSN» returned message «[DataDirect][ODBC OpenAccess SDK driver][OpenAccess SDK SQL Engine]Base table:sysapproval_approver not found.[10129]». [SQLSTATE 01000] (Message 7412) OLE DB provider «MSDASQL» for linked server «SNOWDSN» returned message «[DataDirect][ODBC OpenAccess SDK driver][OpenAccess SDK SQL Engine]Base table:problem not found.[10129]». [SQLSTATE 01000] (Message 7412). The step succeeded.

I am facing a similar problem , did u get the above error resolved ?

Источник

I’m having some trouble configuring a SQL statement to perform an openquery on LDAP from SQL Server. I followed the instructions as laid out here: Querying Active Directory from SQL Server 2005 but I’m having some trouble putting the final pieces together.

Firstly, I didn’t know where my LDAP server was. So I did a nslookup and found the default server as:

abc.domain.popo.local

I configured my OPENQUERY as

SELECT * FROM OPENQUERY( ADSI, 'SELECT * FROM ''LDAP://DC=abc,DC=domain,DC=popo,DC=local'' WHERE
objectCategory = ''User''')   

However, I get an error saying that

An error occurred while preparing the query «SELECT * FROM ‘LDAP://DC=abc,DC=domain,DC=popo,DC=local’ WHERE objectCategory = ‘User'» for execution against OLE DB provider «ADSDSOObject» for linked server «ADSI».

What is the likely issue here? Am I setting up the DC incorrectly (because I don’t even know what DC means)? Or is it more likely that I just have the wrong server altogether for LDAP?

I have been trying to configure a linked server to AD and have found plenty of write ups on how to do it, but have had zero luck with getting it to work.  I also read that the ability was removed after SQL 2008; is that correct?  I am running 2014
and am seeing the following error trying to expand the tree:

TITLE: Microsoft SQL Server Management Studio
——————————

Failed to retrieve data for this request. (Microsoft.SqlServer.Management.Sdk.Sfc)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&LinkId=20476

——————————
ADDITIONAL INFORMATION:

An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)

——————————

Cannot obtain the required interface («IID_IDBSchemaRowset») from OLE DB provider «ADSDSOObject» for linked server «ADSI». (Microsoft SQL Server, Error: 7301)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft%20SQL%20Server&ProdVer=12.00.4100&EvtSrc=MSSQLServer&EvtID=7301&LinkId=20476

Then the following error when trying to query:

SELECT * FROM OpenQuery(ADSI, ‘SELECT displayName FROM »LDAP://<DOMAIN>/DC=DOMAIN,DC=<DOMAIN>» WHERE objectCategory=»User» ‘)

Msg 7321, Level 16, State 2, Line 1
An error occurred while preparing the query «SELECT displayName FROM ‘LDAP://—-/DC=—-,DC=—-‘ WHERE objectCategory=’User’ » for execution against OLE DB provider «ADSDSOObject» for linked server «ADSI». 

The linked server is set for ‘Be made by the login’s current security context’ and local to remote is configured SA to a domain account we use for lookups.  

It does not seem to matter if I am logged in to the server with SQL or Windows credentials.  Thank you in advance for any guidance!


-Drew

When I run this query:

SELECT *   
FROM OPENQUERY([XXX], 'SELECT * 
                       FROM Database.Table 
                       WHERE (MBCONO=650) AND MBCUNO LIKE a%' )

I get the error:

OLE DB provider «DB2OLEDB» for linked server «XXX» returned message
«Token %ŸFOR SKIP WITH FETCH ORDER UNION EXCEPT OPTIMIZE SQLSTATE:
42601, SQLCODE: -104».

Msg 7321, Level 16, State 2, Line 1 An error
occurred while preparing the query «SELECT * FROM Database.Table WHERE
(MBCONO=650) AND MBCUNO LIKE a%'» for execution against OLE DB
provider «DB2OLEDB» for linked server «LAWSON».

I can run the same query without AND MBCUNO LIKE a% successfully.

Mark Storey-Smith's user avatar

asked Apr 10, 2013 at 9:12

A.elm5zngy's user avatar

7

I would suggest you change your original SQL Statement as follows:

SELECT * 
FROM OPENQUERY([XXX], 'SELECT * FROM database.table where left(MBCUNO, 1) = ''a'' and (MBCONO=650) ' )

This has the two variable swopped around, and the like % exchanged for a left(mbcuno,1).

Ensure that all your table & column references are correct

answered Apr 10, 2013 at 12:27

RoKa's user avatar

RoKaRoKa

1,61411 silver badges16 bronze badges

Доброго дня. Решил что опыт обработки данных AD посредством SQL будет полезен не только мне, тем более внятного мануала на русском я так и не нашел.

За стиль изложения прошу сильно не бить, старался объяснить как можно доступнее, как сам хотел увидеть когда искал инфу.

Собственно задача: «А сделайте нам отчет по всем пользователям компании, да так чтобы и группы его выводились». Не знаю насколько это реально какими то нативными средствами администрирования AD, но первая идея пришедшая в голову написать запрос к AD и вытянуть с него всю инфу которую только можно в какую нить табличку и дальше уже обрабатывать чем душе угодно, будь то reporting service или что нить в духе crystal reports. Как же достучаться до AD?

Оказывается у MSSQL есть нативный OLE DB провайдер для подключения AD. Добавляем наш linked server в server objects:

EXEC master.dbo.sp_addlinkedserver @server = N'ADSI'
, @srvproduct=N'Active Directory Service Interfaces'
, @provider=N'ADSDSOObject'
, @datasrc=N'adsdatasource'

Сразу хотел бы отметить что код, используемый в топике, уже не раз опробован за последние несколько дней на разных серверах (2008/2008 R2/2012) и приводится генерируемый sql скрипт.

Как через gui это сделать не скажу, ибо не знаю ;)

После того как мы создали сервер необходимо добавить к нему логин:

EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'ADSI'
,@useself=N'False'
,@locallogin=NULL
,@rmtuser=N'Domainuser'
,@rmtpassword='password'

Хочу заметить, что в настройках самого сервера мы явно не указываем интересующее нас конкретное ldap подключение.

Настройки генерируемые sql-сервером:

EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'collation compatible', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'data access', @optvalue=N'true'
EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'dist', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'pub', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'rpc', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'rpc out', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'sub', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'connect timeout', @optvalue=N'0'
EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'collation name', @optvalue=null
EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'lazy schema validation', @optvalue=N'false'
EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'query timeout', @optvalue=N'0'
EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'use remote collation', @optvalue=N'true'
EXEC master.dbo.sp_serveroption @server=N'ADSI', @optname=N'remote proc transaction promotion', @optvalue=N'true'

Все, сервер у нас есть. Теперь нужен запрос. Для запросов можно использовать два диалекта:

  1. 1) SQL подобный синтаксис — его и использовал.
  2. 2) LDAP подобный синтаксис — на мой взгляд более логично выглядящий, но узнал я о нем уже когда все сделал.

Запросы выполняются через OPENQUERY к интересующему нас серверу. В запрос в FROM пишем ссылку ввида LDAP://dc=maindomain,dc=rootfolder, заворачиваем её в апосторофы и пишем селект лист. Ссылку эту можно получить либо у админов, либо самому поискать по ad нужную нам ветку любым удобным браузером ldap.
Запрос:

SELECT * FROM OPENQUERY(ADSI,'SELECT cn,sAMAccountName FROM ''LDAP://dc=maindomain,dc=rootfolder''')

Жмем f5. Ура запрос работает. Радость недолгая, спустя строк 900 (меньше секунды) вываливается

Cannot fetch a row from OLE DB provider «ADSDSOObject» for linked server «ADSI».


Сразу скажу что это настраиваемое ограничение самого AD, а не драйвера, или провайдера, или еще чего нибудь. Ну если он только не 2000, в 2000 это не настраивается. Как это настраивается можем посмотреть здесь.
Можем пойти двумя путями: попросить админов перенастроить или обойти это ограничение. Конечная цель получить всех пользователей.
Сузим круг поиска:

SELECT * FROM OPENQUERY(ADSI,'SELECT cn,sAMAccountName FROM ''LDAP://dc=maindomain,dc=rootfolder'' WHERE AND objectClass=''person'' AND objectClass<>''computer'' ')

Опять много? Ну что ж, значит нужно хитрить. Можно искать конкретного пользователя например. Но нам нужно тогда знать логины всех пользователей тоже не хорошо. Ну найдем хотя бы тех у которых логин начинается на abc. Искать будем по полю sAMAccountName — это поле которое во всех схемах AD вроде как есть, ну если вы не экстремально уникальную схему используете. Если все же экстремальную, тогда обращайтесь к админам пускай найдут её (схему) для вас. Думаю с полем определились. Теперь пошли отличия диалекта от ansi sql:

SELECT * FROM OPENQUERY(ADSI,'SELECT cn,sAMAccountName FROM ''LDAP://dc=maindomain,dc=rootfolder'' WHERE sAMAccountName =''abc*'' AND objectClass=''person'' AND objectClass<>''computer'' ')

Если есть пользователи начинающиеся на abc, то он вам что нибудь вернет:

sAMAccountName cn
abceeeee abceeeee dfdf eee

Да, в обратном порядке. Нет, не знаю почему. Ладно к примеру мы придумаем вложенный цикл с перебором комбинаций букв от a до z и поиск типа sAMAccountName=aa*, потом sAMAccountName=ab*, опустим динамический запрос. К примеру пользователей получили. Теперь нужно группы получить.

Просим:

SELECT * FROM OPENQUERY(ADSI,'SELECT memberOf,cn,sAMAccountName FROM ''LDAP://dc=maindomain,dc=rootfolder'' WHERE sAMAccountName =''abc*'' AND objectClass=''person'' AND objectClass<>''computer'' ')

Получаем:

Cannot get the data of the row from the OLE DB provider «ADSDSOObject» for linked server «ADSI». Could not convert the data value due to reasons other than sign mismatch or overflow.


memberOf может быть несколько, ну логично в принципе. Попросим не группы пользователя, а группы в которых есть пользователь и приджойним всю эту конструкцию. Чтобы получить все группы, ищем группу у которой в member прописан dn нашего искомого пользователя

SELECT * FROM OPENQUERY(ADSI,'SELECT name FROM ''LDAP://dc=maindomain,dc=rootfolder'' WHERE objectCategory = ''Group'' AND member=''CN=username,OU=Departments,dc=maindomain,dc=rootfolder'' ')

Группы получили, что еще вытянуть из них лучше посмотреть в схеме.

Теперь попробуем пользователя «umi(юми китайский департамент)». Ну странный пользователь, не спорю. Но английское имя, а в скобках русское, вполне себе нормально. Ладно, не суть.
Запрос:

SELECT * FROM OPENQUERY(ADSI,'SELECT name FROM ''LDAP://dc=maindomain,dc=rootfolder'' WHERE objectCategory = ''Group'' AND member=''CN=umi(юми китайский департамент),OU=Departments,dc=maindomain,dc=rootfolder'' ')

Ответ тоже получим, если у этого пользователя есть какие нибудь группы. Но если мы экспортируем пользователя откуда нибудь где у нас пользователь зовется какой нибудь русской длинной абракадаброй, тогда строка у нас будет

umi(юми китайский департамент, должность не помню как искать долго, ну просто так)
А на cn у нас ограничение в 64 символа, итого получаем:

umi(юми китайский департамент, должность не помню как искать дол
С точки зрения запроса все нормально, sql ругаться не будет на кривой запрос, но ldap провйдер ругнется и скажет что

An error occurred while preparing the query «тут наш запрос» for execution against OLE DB provider «ADSDSOObject» for linked server «ADSI».


Да, скобки искать нельзя. Сначала я думал что экранировать можно через кавычки, потом думал что через слеш, потом через амперсанд, много думал. Не получается. Ответ пришелся сам по себе: я этого же пользователя попробовал найти в ldap браузере (любого можете взять, думаю все равно пригодится). И при конструировании запроса когда я ввел слеш он сам мне подсказал какие символы можно экранировать.
Список вот:

  1. ( - 28
  2. ) - 29
  3. * - 2A
  4. - 5C
  5. Carriage Return - D
  6. Line Feed - 0A
  7. NUL - 0

Отдельно стоит отметить апостороф, как видете в списке экранирований апострофа нет. С точки хрения AD все нормально, запрос работает, вываливается ошибка именно на обработке запроса провайдером.Экранировать его можно только через sql

Наш dn выглядит так: CN=Luk'yanova Inna (Лукьянова Инна Инновна),OU=Departments,dc=maindomain,dc=rootfolder.

Мы его присвоили каким то образом в @dn. Чтоб не пришлось вам лишний раз сидеть ковырять сколько апострофов поставить вот:

SET @dn = REPLACE(@dn,'''','''''''''')

Ну а дальше, а дальше либо в процедурину, либо просто куда надо запихиваете этот запрос(запросы) и работаете уже с sql-таблицами как вам угодно.

P.S. об ошибках в топике прошу оповещать меня в личку, а не через комментарии.

Msg 7321, Level 16, State 2, Line 68
An error occurred while preparing the query «

Solution:

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/083229e7-58f2-44cf-ae5d-c9acf366818c/sql-2005-ldap-query-error-msg-7321-level-16?forum=transactsql

It normally succeedes test connection generally but when you run query using this linked server to pull data from AD(Active Directory) server it will throw above issue. so issue will be between db server of linked server and AD server.

we need to make sure logins should be same on both AD server and db server
of linked server created.

LDAP means Lightweight Directory Access Protocol

First execute your query:

EXEC sp_addlinkedserver ‘ADSI’, ‘Active Directory Service Interfaces’,

‘ADSDSOObject’

, ‘adsdatasource’

GO

Like daw21 explain before follow the detail steps:

Second step go to the SqlServer management Studio and edit manually the Linkserver name ADSI:

go to Server Objects/Linked Servers/  and right click on ADSI  select the option Properties and then go to Security.

Select the option :         Be made using this security context:

and there fill the user  and Password about the valid user with permission into Active Directory, not forget before the domainuser.

(or)

First you have to make sure that Ad Hoc Distributed Queries is enabled …

sp_configure ‘Ad Hoc Distributed Queries’, 1
reconfigure
Second you have to make sure that you specify a security context that has access to AD INCLUDING the DOMAIN
Third you should put a row limit (e.g. SELECT TOP 100) in to avoid the issue where you hit the 1000 row limit so
SELECT

TOP 100 *

FROM

OpenQuery(ADSI, ‘SELECT sAMAccountName FROM »LDAP://DC=core,DC=acllab,DC=net» WHERE objectCategory=»user» ‘)

I am getting an error running a query(it also generates a stack dump): SQL 2008R2 standard 10.50.2868.0 NT x64

Msg 7399, Level 16, State 1, Line 2

The OLE DB provider «OraOLEDB.Oracle» for linked server «ZEZASQL» reported an error. The provider reported an unexpected catastrophic failure.

Msg 7321, Level 16, State 2, Line 2

An error occurred while preparing the query «SELECT PE.*

select *

FROM OPENQUERY(ZEZASQL, ‘SELECT PE.*

FROM AWACSUSER.COMPANY CO

JOIN AWACSUSER.COMPANYLOC CL ON CL.CLCOMPANYMDK = CO.COID

JOIN AWACSUSER.MAPPEDCOMPANYMANAGER MC ON MC.MCOMACOMPANYMDK = CL.CLID

JOIN AWACSUSER.PERSON PE ON PE.PEID = MC.MCOMAPERSONMDK

LEFT JOIN AWACSUSER.MGRPERSON MP ON MP.MPPERSONMDK = PE.PEID

LEFT JOIN PERLIVE.RF_MANAGER_STATUS MS ON MS.MANAGER_STATUS_ID = MP.MPCLIENTROLEMDK

LEFT JOIN AWACSUSER.LOOKUPPOSITIONTITLE LP ON LP.LPTID = PE.PETITLEMDK

WHERE PE.PEID = 0 OR (CL.CLID = 1020095 AND PE.PECLIENT = »Yes») ‘)

If i run it with explicit column references it does work. the query works on all our other servers.

First question: Since i am getting a SQL and not an Oracle, does that mean it is a SQL error?

Any input would be appreciated.

g.britton

SSChampion

Points: 13862

Looks like you need to talk to Oracle

TryingToLearn

SSCrazy

Points: 2645

If i have 10 other SQL servers making the same call how can it be an Oracle issue? Possible the Oracle client on that Sql Server. and making explicit columns calls also works…strange

Whenever you get the issue “Msg 7321” make sure your security context is correctly supplied. Otherwise you will get below error.

Msg 7321, Level 16, State 2, Line 1
An error occurred while preparing a query for execution against OLE DB provider 'ADsDSOObject'. OLE DB error trace [OLE/DB Provider 'ADsDSOObject' ICommandPrepare::Prepare returned 0x80040e14].

We should use domain user account in sp_addlinkedserver stored procedure that has access rights to Active Directory. The domain name is required in the Security settings for the remote login. So in the Security settings I entered Remote login as domain user with password. Find below dummy example.

Step1: Run below command.

EXEC sp_addlinkedserver @server = N'ADSI', @srvproduct=N'Active Directory Services', @provider=N'ADsDSOObject', @datasrc=N'LINLDP2'

Output: Success

Step2: Execute below command now.

EXEC sp_addlinkedsrvlogin @rmtsrvname ='ADSI', @useself='false', @rmtuser='DomainNameUserName', @rmtpassword='#Security@SWIM#'

Also, Make sure your SQL Server Service is running under the account which has appropriate access.

Related Articles:

  • How to Improve Bulk Data Load Performance in SQL Server?
  • SQL Server Backup and Recovery Interview Questions

If you like this tip, you can follow us on our facebook page and on Twitter handle to get latest updates.

  • Author
  • Recent Posts

Manvendra Deo Singh

I am working as a Technical Architect in one of the top IT consulting firm. I have expertise on all versions of SQL Server since SQL Server 2000. I have lead multiple SQL Server projects like consolidation, upgrades, migrations, HA & DR. I love to share my knowledge. You can contact me on my social accounts for any consulting work.

Manvendra Deo Singh

Понравилась статья? Поделить с друзьями:
  • An error occurred while preparing the installation try running this application again hackintosh
  • An error occurred while preparing the installation mac os sierra
  • An error occurred while preparing for installation autodesk
  • An error occurred while performing this operation фейсит
  • An error occurred while performing this operation переводчик