Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


SQL Server: how to write variable value into the file?

Posted on 2014-08-28
Medium Priority
Last Modified: 2014-09-23
how to write variable value into the file instead of Results or Messages windows?

Question by:quasar_ee
LVL 75

Expert Comment

by:Anthony Perkins
ID: 40293237
From within SSMS you have to choose the output as Text, Grid or File.  This is a function of the client tool (SSMS) and not SQL Server, so it is an all or nothing approach you cannot pick and choose.
LVL 31

Assisted Solution

by:Rich Weissler
Rich Weissler earned 1000 total points
ID: 40293258
Putting aside the possibilities of calling a windows command shell from within SQL, the only other option I can think of would be to create your own procedure in another language and link it into SQL.  There isn't native support in T-SQL.
LVL 40

Accepted Solution

Vadim Rapp earned 1000 total points
ID: 40293505
EXEC  sp_OACreate 'Scripting.FileSystemObject', @Object OUT
EXEC sp_OAMethod @Object , 'OpenTextFile' , @FileID OUT , 'c:\myfile.txt' , 2 , 1
EXEC sp_OAMethod @FileID , 'WriteLine' , Null , @myvariable
Exec sp_OADestroy @FileID

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question