0

How do I turn the below query into a SQL view? I am trying to get it to a point so that it can be queried when using the SSRS Data subscription. It doesn't necessarily have to be a SQL view if something else is appropriate. When I tried to create the SQL view, I got the below error message

Error Source: .Net SqlClient Data Provider
Error Message: Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.
Incorrect syntax near 'GO'.

--Hitting row limitations at ''s'' > chokes on services
DROP TABLE #tmpADUsers;

CREATE TABLE #tmpADUsers
(
  employeeId VARCHAR(10) NULL,
  SAMAccountName VARCHAR(255) NOT NULL,
  givenname VARCHAR(255) NULL,
  sn VARCHAR(255) NULL,
  email VARCHAR(255) NULL,
  CN VARCHAR(255) NULL
);

GO
-- loop through the alphabet from A-R

DECLARE @cmdstrar VARCHAR(255);
DECLARE @nAsciiValuear SMALLINT;
DECLARE @sCharar CHAR(1);

SELECT @nAsciiValuear = 65;

WHILE @nAsciiValuear < 83
BEGIN

  SELECT @sCharar = CHAR(@nAsciiValuear);

  EXEC master..xp_sprintf @cmdstrar OUTPUT,
          '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*'''''' )',
          @sCharar;

  INSERT #tmpADUsers
  EXEC (@cmdstrar);

  SELECT @nAsciiValuear = @nAsciiValuear + 1;
END;

-- loop through the alphabet from T-Z
GO
DECLARE @cmdstrtz VARCHAR(255);
DECLARE @nAsciiValuetz SMALLINT;
DECLARE @sChartz CHAR(1);

SELECT @nAsciiValuetz = 84;

WHILE @nAsciiValuetz < 91
BEGIN

  SELECT @sChartz = CHAR(@nAsciiValuetz);

  EXEC master..xp_sprintf @cmdstrtz OUTPUT,
          '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*'''''' )',
          @sChartz;

  INSERT #tmpADUsers
  EXEC (@cmdstrtz);

  SELECT @nAsciiValuetz = @nAsciiValuetz + 1;
END;

-- pull in s followed by an alpha
GO
DECLARE @cmdstrsa VARCHAR(255);
DECLARE @nAsciiValuesa SMALLINT;
DECLARE @sCharsa CHAR(1);

SELECT @nAsciiValuesa = 84;

WHILE @nAsciiValuesa < 91
BEGIN

  SELECT @sCharsa = CHAR(@nAsciiValuesa);

  EXEC master..xp_sprintf @cmdstrsa OUTPUT,
          '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%s*'''''' )',
          @sCharsa;

  INSERT #tmpADUsers
  EXEC (@cmdstrsa);

  SELECT @nAsciiValuesa = @nAsciiValuesa + 1;
END;

GO
--this pulls in s with periods as the 2nd character
DECLARE @cmdstrs2 VARCHAR(255);
DECLARE @nAsciiValues2 SMALLINT;
DECLARE @sChars2 CHAR(1);

SELECT @nAsciiValues2 = 65;

WHILE @nAsciiValues2 < 91
BEGIN

  SELECT @sChars2 = CHAR(@nAsciiValues2);

  EXEC master..xp_sprintf @cmdstrs2 OUTPUT,
          '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.%s*'''''' )',
          @sChars2;

  INSERT #tmpADUsers
  EXEC (@cmdstrs2);

  SELECT @nAsciiValues2 = @nAsciiValues2 + 1;
END;

SELECT *
FROM #tmpADUsers
ORDER BY SAMAccountName;
Thom A
  • 88,727
  • 11
  • 45
  • 75
null
  • 713
  • 2
  • 8
  • 30
  • `GO` is an SSMS keyword; it isn't a T-SQL operator. Remove the `GO`. – Thom A Oct 08 '19 at 17:49
  • Almost none of that can be in a view. A "View" is a single select statement. You cannot have variables, do inserts, execute other stored procedures, etc. within a view. – pmbAustin Oct 08 '19 at 19:12
  • Yeah, I thought removing the GO would solve my problems. I used a stored procedure and that works. Except now I cannot use it in a select statement. I cannot start using the Table-valued function either. Can someone suggest a better way? – null Oct 08 '19 at 19:19

0 Answers0