SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Query help to Split AD data


SQL Query help to Split AD data

Author
Message
sqldba20
sqldba20
SSC-Enthusiastic
SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)

Group: General Forum Members
Points: 159 Visits: 445
Folks:

I need help with a SQL Query. I have this data extracted from AD and have in a SQL table with single column.

CREATE TABLE #tblADData
(
DomainData nvarchar(2000)
)

insert into #tblADData values ('CN=James,OU=Luxembourg,OU=Italy,OU=Standard users,OU=Users,OU=Managed objects,DC=XYZ,DC=ABC Ltd,DC=fr')
insert into #tblADData values ('CN=Ernst,OU=Luxembourg,OU=Italy,OU=Standard users,OU=Users,OU=Managed objects,DC=XYZ,DC=ABC Ltd,DC=it')
insert into #tblADData values ('CN=Tommy,OU=Luxembourg,OU=Belgium,OU=Standard users,OU=Users,OU=Managed objects,DC=ABSD,DC=XYZ Ltd,DC=ne')
insert into #tblADData values ('CN=Dcruz,OU=Tuscany,DC=PQRST,DC=PQR Ltd,DC=PQRS')
insert into #tblADData values ('CN=Marti Ho,OU=Intl Contacts,OU=Japan,OU=Contacts,OU=Users,DC=ABSD,DC=ne')
insert into #tblADData values ('CN=Miranda,OU=Montreal,DC=PQR,DC=ABC Ltd,DC=ABSD')
insert into #tblADData values ('CN=Will Hay,OU=Local Contacts,OU=Paris,OU=Contacts,OU=Users,DC=XYZ,DC=fr')



I want to split the data into seperate columns (8 columns).


In some cases if you notice the OU is reflected only once or twice instead of the standard 5 times and the DC is reflected only once instead of standard 2 times. If I use the export utility then the problem is because there are no extra commas it adds the data to a differnt column. I would want the output to be like this in the


CREATE TABLE #tblSplitADData
(
CN VARCHAR(200),
OU1 VARCHAR(200),
OU2 VARCHAR(200),
OU3 VARCHAR(200),
OU4 VARCHAR(200),
OU5 VARCHAR(200),
DC1 VARCHAR(200),
DC2 VARCHAR(200)
)

INSERT INTO #tblSplitADData values ('James','Luxembourg','Italy','Standard users','Users','Managed Objects','XYZ','fr')
INSERT INTO #tblSplitADData values ('Ernst','Luxembourg','Italy','Standard users','Users','Managed Objects','XYZ','it')
INSERT INTO #tblSplitADData values ('Tommy','Luxembourg','Belgium','Standard users','Users','Managed Objects','ABSD','ne')
INSERT INTO #tblSplitADData values ('Dcruz','Tuscany','','','','','PQRS','')
INSERT INTO #tblSplitADData values ('Marti','Intl Contacts','Japan','Contacts','Users','','ABSD','ne')
INSERT INTO #tblSplitADData values ('Miranda','Montreal','','','','','ABSD','fr')
INSERT INTO #tblSplitADData values ('Will Hay','Local Contacts','Paris','Contacts','Users','','XYZ','fr')





Thanks !
Lowell
Lowell
SSC Guru
SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)SSC Guru (74K reputation)

Group: General Forum Members
Points: 74707 Visits: 40985
it might be easier to just query the active directory from SQL via a linked server.

then the pieces can be pulled out automatically;

does this help you at all? it just worked on my domain:

--sp_dropserver ADSI
EXEC master.dbo.sp_addlinkedserver @server = N'ADSI',
@srvproduct=N'Active Directory Services', @provider=N'ADsDSOObject',
@datasrc=N'MyDomain'

select * from openquery
(ADSI,'SELECT
cn,
sn,
SamAccountName,
mail,
telephoneNumber,
mobile,
co,
distinguishedName,
displayName,
physicalDeliveryOfficeName,
department
FROM ''LDAP://MyDomain''
WHERE objectCategory = ''Person'' AND objectClass = ''user'''
)



Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
sqldba20
sqldba20
SSC-Enthusiastic
SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)

Group: General Forum Members
Points: 159 Visits: 445
I know pulling the data directly from AD would have been easier but we are getting this data (flat file) from some other source (1 time activity) and don't have access to their network.
David Webb-CDS
David Webb-CDS
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4214 Visits: 8586
Lowell,

Querying AD directly has always given me a problem when extracting things that have multiple occurrances (like OU or group membership). Is there a way to get those types of data items from a linked server?



And then again, I might be wrong ...
David Webb
Evil Kraig F
Evil Kraig F
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21433 Visits: 7660
Offhand, I would recommend normalizing this data if you have any intentions of searching for groups or similar. The reason being group xyz could be in the 3rd, 4th, or 5th position, making searching very difficult via this model.

Either way, though, you're looking at stripping the string down, determining the individual entries, then choosing what to do with them. If you were to normalize it, I'd recommend the following:

First, you want the delimitedSplit8k. Found here: Tally oh! article

Once you've got that, your code would look like this:

/*
DROP TABLE #tblADData
GO

CREATE TABLE #tblADData
(
RowID INT IDENTITY( 1,1),
DomainData nvarchar(2000)
)

insert into #tblADData values ('CN=James,OU=Luxembourg,OU=Italy,OU=Standard users,OU=Users,OU=Managed objects,DC=XYZ,DC=ABC Ltd,DC=fr')
insert into #tblADData values ('CN=Ernst,OU=Luxembourg,OU=Italy,OU=Standard users,OU=Users,OU=Managed objects,DC=XYZ,DC=ABC Ltd,DC=it')
insert into #tblADData values ('CN=Tommy,OU=Luxembourg,OU=Belgium,OU=Standard users,OU=Users,OU=Managed objects,DC=ABSD,DC=XYZ Ltd,DC=ne')
insert into #tblADData values ('CN=Dcruz,OU=Tuscany,DC=PQRST,DC=PQR Ltd,DC=PQRS')
insert into #tblADData values ('CN=Marti Ho,OU=Intl Contacts,OU=Japan,OU=Contacts,OU=Users,DC=ABSD,DC=ne')
insert into #tblADData values ('CN=Miranda,OU=Montreal,DC=PQR,DC=ABC Ltd,DC=ABSD')
insert into #tblADData values ('CN=Will Hay,OU=Local Contacts,OU=Paris,OU=Contacts,OU=Users,DC=XYZ,DC=fr')
*/

;WITH cte AS
(SELECT
*
FROM
#tblADData AS a
CROSS APPLY
dbo.DelimitedSplit8k( a.DomainData, ',') AS split
)

SELECT
STUFF( c1.Item , 1, 3, '') AS NameOfUser,
LEFT( c2.Item, 2) AS AttributeType,
STUFF( c2.Item, 1, 3, '') AS AttributeValue
FROM
cte AS c1
JOIN
cte AS c2
ON c1.RowID = c2.RowID
WHERE
LEFT( c1.Item, 2) = 'CN'
AND LEFT( c2.Item, 2) <> 'CN'



EDIT: Forgot to mention that you need to adjust your source table to have an easy row identifier.


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)SSC Guru (221K reputation)

Group: General Forum Members
Points: 221394 Visits: 42002
Lowell (12/18/2013)
it might be easier to just query the active directory from SQL via a linked server.

then the pieces can be pulled out automatically;

does this help you at all? it just worked on my domain:

--sp_dropserver ADSI
EXEC master.dbo.sp_addlinkedserver @server = N'ADSI',
@srvproduct=N'Active Directory Services', @provider=N'ADsDSOObject',
@datasrc=N'MyDomain'

select * from openquery
(ADSI,'SELECT
cn,
sn,
SamAccountName,
mail,
telephoneNumber,
mobile,
co,
distinguishedName,
displayName,
physicalDeliveryOfficeName,
department
FROM ''LDAP://MyDomain''
WHERE objectCategory = ''Person'' AND objectClass = ''user'''
)



Crud. Apparently, that doesn't work in 2005?



Msg 7321, Level 16, State 2, Line 1
An error occurred while preparing the query "SELECT
cn,
sn,
SamAccountName,
mail,
telephoneNumber,
mobile,
co,
distinguishedName,
displayName,
physicalDeliveryOfficeName,
department
FROM 'LDAP://MyDomain'
WHERE objectCategory = 'Person' AND objectClass = 'user'" for execution against OLE DB provider "ADsDSOObject" for linked server "ADSI".


An, yeah... I tried changing "MyDomain" to the name of my domain and it produced the same error. Way too bad for me Crying because this would be wicked useful for the internal security audit we're getting ready to do.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search