Question

I have following table:

1    One    TEXT_ONE
2    Two    TEXT_TWO
3    Three  TEXT_Three ...

I want run SQL Query that will creates txt files in specific folder:

C:\Files\One.txt (Text inside - TEXT_ONE)
C:\Files\Two.txt (Text inside - TEXT_TWO)
C:\Files\Three.txt (Text inside - TEXT_Three)...

I'm not so good in SQL, so any help appreciates=)

Thanks

Was it helpful?

Solution

I found my own way. It's probably not absolute answer for my question, but the idea completed.

So, I've created bcp Command that needs to be executed from Command Line

'bcp "SELECT ColumnName FROM DBName.schema.TableName WHERE IDCol = ' + CAST(IDCol as varchar(2)) + '" QUERYOUT .\' + ColumnForFileName + '.txt -FullServerName -T -c'

You can run select statement in SQL. Here is my example:

  SELECT sp.[SessionPageID]
      ,sp.[SummaryBody]
      ,sp.[MainTableBody]
      ,sf.[FileName]
      ,'bcp "SELECT SummaryBody FROM WLS_3E_TIMELOAD_APP.dbo.SessionPage WHERE SessionPageID = ' + CAST(sp.[SessionPageID] as varchar(2)) + '" QUERYOUT .\SB_' + sf.[FileName] + '_Session.txt -SCHAKHAU-T430\CHAKHAU -T -c'
      ,'bcp "SELECT MainTableBody FROM WLS_3E_TIMELOAD_APP.dbo.SessionPage" QUERYOUT .\MB_' + sf.[FileName] + '_Session.txt -MYLapTOP\LOCAL -T -c'
  FROM [SessionPage] sp
  JOIN SourceFile sf ON sf.SourceFileID = sp.SourceFileID

If you run this kind of query in result you will get all your bcp commands, so you then can execute them from Command Line

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top