Home All Groups Group Topic Archive Search About
Author
30 Jun 2006 4:31 PM
A McGuire
Hello,
I'm trying to loop through a cursor, which is essentially querying the server names of the SQL Servers I manage.  I pretty much understand this won't work, but many of you will understand what I'm attempting to do.  Is there another way to approach this that will work?  This will basically allow me to automate the process connecting to various servers versus having to use the tedious process of clicking "Query -> Connection -> Change Connection".

I'm currently running this script through the SMSE in SQLCMD mode.  I don't object to doing it another way.


declare @servername varchar(255)
DECLARE servercur CURSOR FOR
    SELECT ServerName FROM tServers
OPEN servercur
FETCH NEXT FROM servercur INTO @servername
WHILE @@FETCH_STATUS = 0 BEGIN
    :connect ' + @servername
    SELECT CAST (@@version AS char (175))
    PRINT @@version
    GO
FETCH NEXT FROM servercur INTO @servername
END
CLOSE servercur
DEALLOCATE servercur

Thank you in advance,
Allen

Author
30 Jun 2006 5:42 PM
Arnie Rowland
Procedural programming 'should' not happen in SQL Server -but sometimes it does what is needed -especially for maintenance.

Don't use a CURSOR when a WHILE loop will do the task. Something like this could work. (Assumes you have a table of Servers and that table has an identity column.

DECLARE
     @ServerName nvarchar(100)
   , @RowCount   int
   , @TotalRows  int

SET @RowCount = 0

SELECT @TotalRows = count(1)
FROM   MyServerList

WHILE @RowCount < @TotalRows
   BEGIN
      SELECT @ServerName = ServerName
      FROM   MyServerList
      WHERE  MyServerList_ID = @RowCount

      {Do your work here

      SET @RowCount = (@RowCount + 1)
   END
    ,

--
Arnie Rowland, YACE*
"To be successful, your heart must accompany your knowledge."

*Yet Another Certification Exam


  "A McGuire" <allen.mcgu***@gmail.com.invalid> wrote in message news:%23Qs4hJGnGHA.4296@TK2MSFTNGP02.phx.gbl...
  Hello,
  I'm trying to loop through a cursor, which is essentially querying the server names of the SQL Servers I manage.  I pretty much understand this won't work, but many of you will understand what I'm attempting to do.  Is there another way to approach this that will work?  This will basically allow me to automate the process connecting to various servers versus having to use the tedious process of clicking "Query -> Connection -> Change Connection".

  I'm currently running this script through the SMSE in SQLCMD mode.  I don't object to doing it another way.


  declare @servername varchar(255)
  DECLARE servercur CURSOR FOR
      SELECT ServerName FROM tServers
  OPEN servercur
  FETCH NEXT FROM servercur INTO @servername
  WHILE @@FETCH_STATUS = 0 BEGIN
      :connect ' + @servername
      SELECT CAST (@@version AS char (175))
      PRINT @@version
      GO
  FETCH NEXT FROM servercur INTO @servername
  END
  CLOSE servercur
  DEALLOCATE servercur

  Thank you in advance,
  Allen
Author
30 Jun 2006 5:58 PM
A McGuire
The actual crux of my problem, however, is in the SQLCMD statement.  I agree, I could use a WHILE loop versus the CURSOR, but my issue is with dynamically building the SQLCMD statement

:connect ' + @servername

does not work.  What I want is some way to do security setting checks on an array of servers without having to replicate the code over and over after each :connect statement.

I have about 50 servers that I care for and don't want to have to manually connect to each server in order to run my security checking script.
  "Arnie Rowland" <ar***@1568.com> wrote in message news:eE$7SyGnGHA.4816@TK2MSFTNGP04.phx.gbl...
  Procedural programming 'should' not happen in SQL Server -but sometimes it does what is needed -especially for maintenance.

  Don't use a CURSOR when a WHILE loop will do the task. Something like this could work. (Assumes you have a table of Servers and that table has an identity column.

  DECLARE
       @ServerName nvarchar(100)
     , @RowCount   int
     , @TotalRows  int

  SET @RowCount = 0

  SELECT @TotalRows = count(1)
  FROM   MyServerList

  WHILE @RowCount < @TotalRows
     BEGIN
        SELECT @ServerName = ServerName
        FROM   MyServerList
        WHERE  MyServerList_ID = @RowCount

        {Do your work here

        SET @RowCount = (@RowCount + 1)
     END
      ,

  --
  Arnie Rowland, YACE*
  "To be successful, your heart must accompany your knowledge."

  *Yet Another Certification Exam


    "A McGuire" <allen.mcgu***@gmail.com.invalid> wrote in message news:%23Qs4hJGnGHA.4296@TK2MSFTNGP02.phx.gbl...
    Hello,
    I'm trying to loop through a cursor, which is essentially querying the server names of the SQL Servers I manage.  I pretty much understand this won't work, but many of you will understand what I'm attempting to do.  Is there another way to approach this that will work?  This will basically allow me to automate the process connecting to various servers versus having to use the tedious process of clicking "Query -> Connection -> Change Connection".

    I'm currently running this script through the SMSE in SQLCMD mode.  I don't object to doing it another way.


    declare @servername varchar(255)
    DECLARE servercur CURSOR FOR
        SELECT ServerName FROM tServers
    OPEN servercur
    FETCH NEXT FROM servercur INTO @servername
    WHILE @@FETCH_STATUS = 0 BEGIN
        :connect ' + @servername
        SELECT CAST (@@version AS char (175))
        PRINT @@version
        GO
    FETCH NEXT FROM servercur INTO @servername
    END
    CLOSE servercur
    DEALLOCATE servercur

    Thank you in advance,
    Allen

AddThis Social Bookmark Button