4 Replies - 259 Views - Last Post: 05 February 2014 - 06:27 PM

#1 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

SqlExpress: Query Result to OutFile

Posted 05 February 2014 - 06:05 PM

Every search I've done keeps pointing to mySQL but is there a way to redirect a query result to a .txt file with MS SqlServerExpress?.

Something like:
SELECT Name, Addr, City, State, Zip FROM Addresses WHERE Name LIKE 'A%' OUTFILE 'C:\AddressResults.txt'

I know I can redirect and run the query in SSMS but I would like to do this from my application.

I can see the security ramifications of allowing this, but It appears you can do it with mySQL so maybe I'm being paranoid. Anyway, can this be done in MSSqlExpress

Is This A Good Question/Topic? 0
  • +

Replies To: SqlExpress: Query Result to OutFile

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9049
  • View blog
  • Posts: 33,972
  • Joined: 12-June 08

Re: SqlExpress: Query Result to OutFile

Posted 05 February 2014 - 06:06 PM

It would depend on the application.. if it's a .NET app then reading the results into a dataset and writing said dataset to a file is a pretty one-two app.
Was This Post Helpful? 1
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: SqlExpress: Query Result to OutFile

Posted 05 February 2014 - 06:20 PM

Yea, I know this. Was just wondering if it could be done from the query itself.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3320
  • View blog
  • Posts: 11,229
  • Joined: 12-December 12

Re: SqlExpress: Query Result to OutFile

Posted 05 February 2014 - 06:24 PM

I'm with modi ;). I was looking around and it only seems possible from SSMS or a command prompt.

I like to explore though.. so it might work using bcp from a shell:

Quote

bcp "SELECT FirstName, LastName FROM AdventureWorks2008R2.Person.Person ORDER BY LastName, Firstname" queryout Contacts.txt -c -T

You'd have to embed the parameter-value though.

Exporting Data from a Query to a Data File :MSDN
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9049
  • View blog
  • Posts: 33,972
  • Joined: 12-June 08

Re: SqlExpress: Query Result to OutFile

Posted 05 February 2014 - 06:27 PM

There are some options..

SQLCMD
http://technet.micro...y/ms162773.aspx
http://technet.micro...y/ms180944.aspx
http://technet.micro...sql.105%29.aspx

BCP
http://msdn.microsof...y/ms162802.aspx

Powershell:
http://billfellows.b...ery-to-csv.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1