dbTalk Databases Forums  

extracting a query into a text file (equivalent to Oracle's spoolcommand)

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss extracting a query into a text file (equivalent to Oracle's spoolcommand) in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
George Lewycky
 
Posts: n/a

Default extracting a query into a text file (equivalent to Oracle's spoolcommand) - 05-03-2010 , 08:58 AM






Hello.

I’m an Oracle transplant to the SQL Server 2008 adjusting and enjoying
many new features but missing some Oracle features I can’t find the
equivalent too.

One of them being the best “secure” technique to extract a query into
a text file to then be FTP’d to another machine. This will be a daily
scheduled or triggered task.


In my Oracle days a simple “spool” command did the trick

From what I can find so far BCP seems to be best solution, but I’m
trying to keep this in a T-SQL procedure without using the
xp_cmdshell.

BCP.EXE "select CustomerID, CompanyName from Northwind..Customers
where
City='London'" queryout LondonCustomers.csv -c -CACP -t, -T


Any ideas, suggestions or websites would be really appreciated

George

Reply With Quote
  #2  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: extracting a query into a text file (equivalent to Oracle's spoolcommand) - 05-03-2010 , 10:07 AM






There is not a straight forward method to extract data into text file in SQL Server. Especially if you want to do this
from T-SQL and avoid using xp_cmdshell. You can build something using CLR, here is one example:
http://www.mssqltips.com/tip.asp?tip=1662

--
Plamen Ratchev
http://www.SQLStudio.com

Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: extracting a query into a text file (equivalent to Oracle's spool command) - 05-03-2010 , 04:30 PM



George Lewycky (gelewyc (AT) nyct (DOT) com) writes:
Quote:
I’m an Oracle transplant to the SQL Server 2008 adjusting and enjoying
many new features but missing some Oracle features I can’t find the
equivalent too.

One of them being the best “secure” technique to extract a query into
a text file to then be FTP’d to another machine. This will be a daily
scheduled or triggered task.


In my Oracle days a simple “spool” command did the trick

From what I can find so far BCP seems to be best solution, but I’m
trying to keep this in a T-SQL procedure without using the
xp_cmdshell.

BCP.EXE "select CustomerID, CompanyName from Northwind..Customers
where
City='London'" queryout LondonCustomers.csv -c -CACP -t, -T

The best solution may be to use SQL Server Integration Services, but
I have never used SSIS myself.

If this is to be scheduled, you could run it from SQL Server Agent as
a command-line task that invokes BCP.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Reply With Quote
  #4  
Old   
George Lewycky
 
Posts: n/a

Default Re: extracting a query into a text file (equivalent to Oracle's spoolcommand) - 05-03-2010 , 04:48 PM



On May 3, 5:30*pm, Erland Sommarskog <esq... (AT) sommarskog (DOT) se> wrote:
Quote:
George Lewycky (gele... (AT) nyct (DOT) com) writes:
I’m an Oracle transplant to the SQL Server 2008 adjusting and enjoying
many new features but missing some Oracle features I can’t find the
equivalent too.

One of them being the best “secure” technique to extract a query into
a text file to then be FTP’d to another machine. This will be a daily
scheduled or triggered task.

In my Oracle days a simple “spool” command did the trick

From what I can find so far BCP seems to be best solution, but I’m
trying to keep this in a T-SQL procedure without using the
xp_cmdshell.

* * *BCP.EXE "select CustomerID, CompanyName from Northwind..Customers
where
* * *City='London'" queryout LondonCustomers.csv -c -CACP -t, -T

The best solution may be to use SQL Server Integration Services, but
I have never used SSIS myself.

If this is to be scheduled, you could run it from SQL Server Agent as
a command-line task that invokes BCP.

--
Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008:http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005:http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000:http://www.microsoft.com/sql/prodinf...ns/books.mspx- Hide quoted text -

- Show quoted text -


for some reason my manager doesnt want me using SSIS for this task
but ill re-iterate to him

Reply With Quote
  #5  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: extracting a query into a text file (equivalent to Oracle's spool command) - 05-03-2010 , 05:01 PM



George Lewycky (gelewyc (AT) nyct (DOT) com) writes:
Quote:
for some reason my manager doesnt want me using SSIS for this task
but ill re-iterate to him
There may be sound reasons for it. If you don't use SSIS elsewhere,
and this is a tiny fringe thing, adding SSIS adds a whole of complexity
and requirements on staffing. As I said, I'm not using SSIS myself,
but I felt obliged to mention it, since it seems to be the standard
solution for this kind of thing.



--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Reply With Quote
  #6  
Old   
George Lewycky
 
Posts: n/a

Default Re: extracting a query into a text file (equivalent to Oracle's spoolcommand) - 05-05-2010 , 06:10 PM



thanks Erland I'll mention this to my manager

I was hoping for someone from SQL Server & Oracle world to catch this

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.