March 26, 2008 at 10:41 am
I would like to print to a specified text file, e.g. "c:\Temp\tmp.txt", using T-SQL. However, I cannot find a suitable command to do this. Please can someone help me out?
March 26, 2008 at 10:57 am
What are you trying to accomplish?
You can use openrowset to write to a text file.
http://msdn2.microsoft.com/en-us/library/ms190312.aspx
If you need to export data to a text file, you can use the export data wizard or BCP. To initiate the export wizard simply right-click on the database --> tasks --> export data.
more info: bcp
March 26, 2008 at 2:26 pm
I want to write debug statements and error messages to a log. I have tried writing the information to log tables, but if an error occurs in the middle of a transaction, then the rollback causes the log information to be rolled back as well.
If there is a way round this rollback problem, then I wouldn't need to write a text log.
March 26, 2008 at 3:24 pm
You can save your error log data in a table variable. Table variables are nonpersistent and therefore not rolled back. You can insert the table variable data into a permenant table, after the rollback.
March 27, 2008 at 4:23 am
If you have access to extended stored procedure xp_cmdshell, you can use it to write to the text file. So you could use: exec master..xp_cmdshell "echo your_debug_statement >> c:\temp\temp.txt". You may construct the OS command as a variable and then use it as exec master..xp_cmdshell @strCmd. Please note that since this will execute an OS command, there may be a performance impact.
March 28, 2008 at 9:22 am
...or you could use:
sp_OACreate 'Scripting.FileSystemObject'...
sp_OAMethod ...'Write or WriteLine'...
Create a sproc to open the output file then use a function to write lines to it...
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply