Batchfile and storing result

  • Hi,

    I have to use sql cmd and run diagnostic queries.

    I need help to run multiple dmvs as a batch file and storing the dmv result to some place.

    How to start this?

  • write a stored procedure to insert the results of the DMV into a table in your database and then call it from the command line?

  • No.

    They don't want to make any changes to the databases. They need the select query (DMV) out put to the filesystem

  • sqlcmd -S SERVERNAME -d DATABASENAME -W -h -1 -s "|" -i sql_query.txt >output_file.txt

    -W remove trailing spaces

    -h -1 remove column headers

    -s "|" I like to use the vertical pipe as column delimiter.

    -i sql_query.txt put your sql query into the file "sql_query.txt"

    >output_file.txt this is where your output from your query shows up

  • I ran like this

    sqlcmd -S SERVERNAME -d Master -i sql_query.bat -o output_file.txt

    It is working but just I need to change the output format to understand the query result just like in ssms

  • Excel would probably do nicely with formatting, that would be one of my choices. Maybe you could also investigate building html from your query and just outputting that?

  • I tried excel extension also. Still most of the values going out of area and mixing with next column.

  • when you import into excel, do you use a unique column delimiter? I usually am pretty successful when I use a suitable column delimiter.

    You could also consider hooking excel up directly with a query if you know any vba.

  • Do you mean first give the extention as .csv in output and then open using excel?

  • ramana3327 (10/7/2015)


    Do you mean first give the extention as .csv in output and then open using excel?

    Ah thats where I went wrong, thats a good way to do it. Often though, I run the import wizard from "get external data" and spec the column separator during that process.

  • Your option importing from text is working fine.

    Thanks

Viewing 12 posts - 1 through 11 (of 11 total)

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