Counter of Festivals

Ashok Blog for SQL Learners and Beginners and Experts

Monday 10 February 2020

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

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 domain\user.



(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'' ')

Monday 3 February 2020

To change file name of text files from SQL QUERY

----------------To change file name of text files from SQL QUERY-------------------------------------------------------------

create table #test(col   varchar(1000))
create table #files(name varchar(1000),rename varchar(1000))

Declare @path as varchar(500)='\\sharepath\filesfolder\test\'
Declare @ssql as varchar(500)='dir ' + @path
Declare @col as varchar(500)=''
Declare @col1 as varchar(500)=''

insert into #test
Exec xp_cmdshell @ssql


--------Tables

insert into #files
select name=reverse(left(reverse(col),PATINDEX('% %',reverse(col)))),
rename=REPLACE(reverse(left(reverse(col),PATINDEX('% %',reverse(col)))),'.Table','')

-------SPs

--insert into #files
--select name=reverse(left(reverse(col),PATINDEX('% %',reverse(col)))),
--rename=REPLACE(reverse(left(reverse(col),PATINDEX('% %',reverse(col)))),'.StoredProcedure','')


from #test where PATINDEX('%.%',col) > 0

delete
from #files where  name in (' .',' ..')

select @col =name , @col= rename
from #files

while (1=1)
Begin
select  top 1 @col =name , @col1= rename from #files
if @col = ''
begin
break
return
End

set @ssql = 'rename  ' + @path+'\'+ ltrim(rtrim(@col))+' '+ltrim(rtrim(@col1))+''
print @ssql

Exec xp_cmdshell @ssql
delete  from #files  where @col =name
set @col = ''
End


------------file name be like dbo.Test2.Table
-------output of result dbo.Test2