Как использовать CTE для рекурсивной замены параметра в сквозном запросе на связанном сервере? - PullRequest
0 голосов
/ 08 октября 2019

Как заменить A * на переменную, которая может пройти по AZ с помощью CTE?

SELECT * FROM OpenQuery (
ADSI,
'
SELECT sAMACCOUNTNAME,mail
FROM ''LDAP://DC=abc,DC=ad,DC=def''
WHERE objectClass = ''User'' AND sAMACCOUNTNAME=''A*''
')

1 Ответ

0 голосов
/ 09 октября 2019

У меня есть решение просто вручную сделать запрос из AZ. Но я надеюсь на более элегантное решение.

CREATE VIEW [dbo].[ADUpdateData]
AS
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''A*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''B*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''C*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''D*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''E*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''F*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''G*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''H*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''I*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''J*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''K*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''L*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''M*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''N*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''O*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''P*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''Q*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''R*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''S*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''T*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''U*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''V*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''W*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''X*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''Y*'''  )    UNION ALL
SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM OPENQUERY( ADSI, 'SELECT employeeId, SAMAccountName, givenname, sn, Mail, CN FROM ''LDAP://DC=abc,DC=ad,DC=def'' WHERE objectCategory =''Person'' AND SAMAccountName =''Z*'''  );   
GO
...