run a query against all servers...

  • i'm trying to find an sql user on 'all' machines, but don't

    want to look at each one individually.

    is there a way i can run following script against all machines

    in the environment?

    select * from sysusers where [name] = 'user'

    _________________________

  • I don't think ther is any build in function or procedure to do this job..

    Third party tools like DBArtizan will do this job for you...

    OR

    YOu can write your own code...

    1. Create a table and enter all server names...

    2. Write a script with cursor/while loop to execute the script on all servers using OSQL.

     

    MohammedU
    Microsoft SQL Server MVP

  • A simple Windows command line will work:

    for /f %i in (servers.txt) do SQLCMD -S %i -i C:\myscript.sql

  • thanks for the reply, but i'm alittle confused on

    how this works.

    is this what you are saying?

    -------------

    -------------

    create the following files:

    C:\servers.txt Contains the server names.

    C:\myscript.sql Containts the TSQL script.

    then open a command prompt, and type in the

    following:

    /f %i in C:\servers.txt do SQLCMD -S %i -i C:\myscript.sql

    -------------

    -------------

    is this correct?

    _________________________

  • If can run a query against all your databases and servers using third party tools like SQL Farm Combine. You can query hundreds of databases and get a single result set.

    Editor's Note: : Dr. Omri Bahat works for SQLFarms, a software vendor of SQL Server tools.

  • That's correct, however the syntax should be

    for /f %i in (c:\servers.txt) do sqlcmd -S %i -i C:\myscript.sql

     

    Also you could use osql instead of sqlcmd

Viewing 6 posts - 1 through 5 (of 5 total)

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