Davie
Davie

Reputation: 838

Output SQL messages to a Log File

I have an SQL script than runs a couple of DBBC commands. Is there a way to send the messages generated from executing these commands to a simple log file e.g "C:\log.txt"?

The messages I mean are the ones that appear in the messages box at the bottom of SQL Server 2005 Management Studio after a query has executed.

The reason for needing to do this is because I don't intend on having SQL Server Management Studio open so the messages can't be seen.

I am using SQL Server 2005 if that helps.

Upvotes: 7

Views: 26186

Answers (3)

ExploringApple
ExploringApple

Reputation: 1482

For anyone who wasn’t aware SQLCMD is a command line tool for running T-SQL statements and scripts.

so from the command line:

 sqlcmd -i script.sql -S <myserver> -E | tee "C:\log.txt"
  • SQLCMD can be used to run a script or stored procedure on a local/remote SQL Server.

  • SQLCMD is installed with SSMS and with the database engine. And on the machine with only client or application it can be installed from the Command Line Utilities.

  • Supported Operating Systems - Windows 10, Windows 7, Windows 8, Windows 8.1, Windows Server 2008, Windows Server 2008 R2, Windows Server 2008 R2 SP1, Windows Server 2012, Windows Server 2012 R2

Upvotes: 0

Mitch Wheat
Mitch Wheat

Reputation: 300797

In SSMS, click Query->Results To...->Results To File

Upvotes: 5

Raj More
Raj More

Reputation: 48048

You can run scripts from the Command line using SQLCMD utility.

http://msdn.microsoft.com/en-us/library/ms162773%28SQL.90%29.aspx

You can run this through a batch file. It let's you log to a file.

You can automate such jobs using scheduling programs.

Upvotes: 6

Related Questions