Extend Table with new query informations

Hi I have the fallowing

#Tabel1
UserName Information1 Information2 Information3
aaaa ab cv de
bbbb ae rf cs
dddd et hh rt
bbbb ze rt sw

Query
declare @name nvarchar(100)
declare @sql nvarchar(max)

set @name = 'aaaa'
set @sql=
'SELECT * FROM OPENQUERY (
ADSI,
''SELECT 
samaccountname,
RBCountryISOCode , Department, company

FROM ''''LDAP://rbeurdom01/DC=rbdom,DC=rbroot,DC=net''''
WHERE 
objectClass = ''''user'''' 
and objectCategory = ''''person''''
and samaccountname = ''''' + @name + '''''
'')'
exec dbo.sp_executeSQL @sql

I want an result of #TableReslt with

UserName Information1 Information2 Information3 RBCountryISOCode   Department   company
aaaa ab cv de DE Market Company1
bbbb ae rf cs US Sales Company2
dddd et hh rt BE Market Company1
bbbb ze rt sw US Sales Company2

How do i do it? Thank you

March 19th, 2015 7:29pm

Place an INSERT INTO #TableReslt before your SELECT.

Free Windows Admin Tool Kit Click here and download it now
March 19th, 2015 7:36pm

Hello,

Your answer does not help me, that is not what i need. I need the Query that will search for each User value in the first table information  using the Query.
Can someone put them together somehow?

March 20th, 2015 4:34am

How should anybody see this in your ASCII art??

Simply join it or use a IN predicate. E.g.

USE msdb;
GO

CREATE TABLE #Tables ( name SYSNAME );

INSERT  INTO #Tables
VALUES  ( 'MSdbms' ),
        ( 'MSdbms_datatype' ),
        ( 'MSdbms_map' );

DECLARE @Sql NVARCHAR(MAX) = N'
	SELECT  T.*
	FROM    sys.tables T
		INNER JOIN #Tables S ON T.name = S.name;
';

EXECUTE ( @Sql );

SET @Sql = N'
    SELECT  T.*
    FROM    sys.tables T
    WHERE   T.name IN ( SELECT  S.name
                        FROM    #Tables S );
';

EXECUTE ( @Sql );

DROP TABLE #Tables;

Free Windows Admin Tool Kit Click here and download it now
March 20th, 2015 1:02pm

Something like this i wanted:

Create a procedure from the code that reads data from AD
Create a cursor that goes trough the first table and collect the information.
I did not know about the option to insert type of code..

procedure sp_getADInfo @name nvarchar(100)

declare @nume nvarchar(100)
declare nmsCursor cursor for select distinct nume from #temptbl
open nmsCursor
fetch next from nmsCursor into @nume

WHILE @@FETCH_STATUS = 0
BEGIN

	insert into #tblPopulatedFromAD
	exec sp_getADInfo @nume

	fetch next from nmsCursor into @nume

END

close nmsCursor
deallocate nmsCursor

select * from  #temptbl a
left join  #tblPopulatedFromAD b
on a.nume = b.displayName


March 21st, 2015 3:40am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics