Getting User's OU from Active Directory via TSQL Stored Procedure

I have this query that works as expected and I get exactly what I want to get from AD:
SELECT SUBSTRING(ADsPath,LEN(LEFT(ADsPath,CHARINDEX(',',ADsPath)+1)),LEN(ADsPath)-LEN(RIGHT(ADsPath,20))-LEN(LEFT(ADsPath,CHARINDEX(',',ADsPath)))) AS OU
FROM OPENQUERY( ADSI, 'SELECT * FROM ''LDAP:// DC=sd_corp,DC=local'' WHERE sAMAccountName = ''CROMER'' ')

Open in new window

Results in one record: OU=ScreensaverOmit.

What I want to do is this:
DECLARE @USER AS VARCHAR(10)
SET @USER='CROMER'

SELECT SUBSTRING(ADsPath,LEN(LEFT(ADsPath,CHARINDEX(',',ADsPath)+1)),LEN(ADsPath)-LEN(RIGHT(ADsPath,20))-LEN(LEFT(ADsPath,CHARINDEX(',',ADsPath)))) AS OU
FROM OPENQUERY( ADSI, 'SELECT * FROM ''LDAP:// DC=sd_corp,DC=local'' WHERE sAMAccountName = ''' + @USER + ''' ')

Open in new window

I get this error when I run it using the variable: "Msg 102, Level 15, State 1, Line 5
Incorrect syntax near '+' ". I can't figure out where the error is.
Carla RomereDirector of Information TechnologyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
DECLARE @USER AS VARCHAR(10)
SET @USER='CROMER'

declare @query VARCHAR(1000)
SET @query = '
SELECT SUBSTRING(ADsPath,LEN(LEFT(ADsPath,CHARINDEX('','',ADsPath)+1)),LEN(ADsPath)-LEN(RIGHT(ADsPath,20))-LEN(LEFT(ADsPath,CHARINDEX('','',ADsPath)))) AS OU
FROM OPENQUERY( ADSI, ''SELECT * FROM ''''LDAP:// DC=sd_corp,DC=local'''' WHERE sAMAccountName = ''''' + @USER +   ''''''')'

EXEC(  @qUERY )
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Carla RomereDirector of Information TechnologyAuthor Commented:
That was fast and worked perfectly. Thank you.
0
Brian CroweDatabase AdministratorCommented:
try this...

DECLARE @OpenQuery NVARCHAR(1000)
SET @OpenQuery = 'SELECT SUBSTRING(ADsPath,LEN(LEFT(ADsPath,CHARINDEX('','',ADsPath)+1)),LEN(ADsPath)-LEN(RIGHT(ADsPath,20))-LEN(LEFT(ADsPath,CHARINDEX('','',ADsPath)))) AS OU
FROM OPENQUERY( ADSI, ''SELECT * FROM ''''LDAP:// DC=sd_corp,DC=local'''' WHERE sAMAccountName = ''''' + @USER + ''''' '')'

EXECUTE sp_executesql @openquery

Open in new window

0
Brian CroweDatabase AdministratorCommented:
oops...i was a little late :-)
0
Carla RomereDirector of Information TechnologyAuthor Commented:
No worries, BriCrowe - I appreciate the help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.