Friday, March 9, 2012

Reference a flat file (as a table) in a sql statemtent

Several years ago, I am sure that I had a query that either read from or wrote to a text file. There was some way to:
select * into 'c:\\out.txt' from SomeTable
Does anyone know if this functionality exists? If so, what is the syntax?
TIAYou might be able to get what you are looking for using a text file linked server using sp_addlinkedserver (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_sp_adda_8gqa.asp) (it is example H). You might also get what you want using OSQL.EXE (http://msdn.microsoft.com/library/en-us/coprompt/cp_osql_1wxl.asp) too.

-PatP|||Originally posted by Pat Phelan
You might be able to get what you are looking for using a text file linked server using sp_addlinkedserver (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_sp_adda_8gqa.asp) (it is example H). You might also get what you want using OSQL.EXE (http://msdn.microsoft.com/library/en-us/coprompt/cp_osql_1wxl.asp) too.

-PatP

Set up a linked server, then use SELECT * FROM OPENQUERY(linked_server,file) or whatever else you want to do.|||I feel OSQL is better tool to use than Linked server.|||Originally posted by Satya
I feel OSQL is better tool to use than Linked server.

Why? And can you give the guy an example of how this would work for him, including how he'll set up the needed datasource.|||Thanks for the replies. It doesn't look like it is possible to use the method I thought I'd remembered. Your way is more than sufficient. Thanks.|||ummmm...bcp with queryout?|||Queryout? I did use bulk copy in a query to import a fixed width flat file using a format file, but I don't know what queryout is...|||Well I thought you're talking about outbound...

USE Northwind
GO

EXEC master..xp_cmdshell 'bcp "SELECT * FROM Orders" queryout c:\tax\Test.txt -S\servername\instance -U -P -c'

No comments:

Post a Comment