SQL Server, How to easily create a text file with the result of your query (another easy tips)

Hello friends,
Its the time for another "useful but fast & easy” tips!

The question of today is:
 

I have a query, a select on my database the "give me" a resultset which I need to export to a text file. How to produce the text file? maybe this operation must be schedulable.

So, How to do it? 

We can take advantage of the SqlCmd command that we have already used here: execute T-SQL commands from the command line
 
Just prepare your select as in the picture below and save it into a .SQL file.
 

 
Then open a command promp and type:

SqlCmd -U xx -P yyyyy -i select.sql -o c:\dati.txt

where 
 
-U username

-P password 

-i the location of your script
 
-o is the file name that will be filled up with your data!  

 

Et voilà! 


 


That's all for today
I wish you a great day!
Luca











Previous post:SQL SERVER, One thing you should definitely know about the UPDATE statement ...

 

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thank you so much.I guess this code snippet can really be helpful to solve complex operations and problems such as bugs and errors.

    <a href="https://zappysys.com/blog/import-rest-api-json-sql-server/> SQL Server Load Soap API </a>

    ReplyDelete

Post a Comment

I Post più popolari

SQL Server, execution plan and the lazy spool (clearly explained)

SQL Server, datetime vs. datetime2

La clausola NOLOCK. Approfondiamo e facciamo chiarezza!