Cursor table update

  • DECLARE @id VARCHAR(10)

    DECLARE myCursor CURSOR LOCAL FAST_FORWARD FOR

    SELECT [ServersList] AS 'ID'

    FROM dbo.Servers

    OPEN myCursor

    FETCH NEXT FROM myCursor INTO @id

    WHILE @@FETCH_STATUS = 0 BEGIN

    exec sp_executesql N'xp_cmdshell ''ping @id'''

    --PRINT @id

    -- do your tasks here

    FETCH NEXT FROM myCursor INTO @id

    END

    How do loop a table server(serverlist,flag) table with these 2 columns.

    And ping each of the servers in the table and update the flag column to '1' if ping has been successfull or flag to '0' if ping has been

    unsuccessfull.Responses are appreciated.

    Can anyone help me with the code.

    The output of the above code is:

    Ping request could not find host @id. Please check the name and try again.

    NULL

  • I think that T-SQL is a horrible tool for this job. Did you consider any alternative, such as a powershell step in a SQL Agent job?

    -- Gianluca Sartori

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply