Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 unable to copy table into text file

Author  Topic 

guna_dev
Starting Member

2 Posts

Posted - 2007-09-17 : 15:31:54
Hi,
I want to query a table and save the recordset into a text file. I used bcp and xp_cmdshell. Query gets executed without any error but text file is not generated (using sql server 2000). But when i run the bcp command in command prompt, text file is generated. Here is the query i tried.

declare @query varchar(1000)
set @query = 'bcp.exe "select employeeid,firstname from northwind.dbo.employees" queryout "D:/nw_employees.txt" -P "sa" -cT -t -T -k'
exec master..xp_cmdshell @query

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-09-17 : 15:38:32
Is the SQL Server on the same box where you are running this query? If not, then the file is being created on the database server and not on your client machine. xp_cmdshell runs from the server. When you run bcp.exe from your own command prompt, it runs from the client.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Zoroaster
Aged Yak Warrior

702 Posts

Posted - 2007-09-17 : 15:40:57
It works for me copied verbatim so I am not sure. You say you were able to copy and paste the exact output as below:

bcp.exe "select employeeid,firstname from northwind.dbo.employees" queryout "d:/nw_employees.txt" -P "sa" -cT -t -T -k

into the command line and it works?

What does the results pane say when you run it in QA?

This is what I get:

output
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
NULL
Starting copy...
NULL
9 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.): total 1
NULL

(7 row(s) affected)





Future guru in the making.
Go to Top of Page

Zoroaster
Aged Yak Warrior

702 Posts

Posted - 2007-09-17 : 15:41:48
quote:
Originally posted by tkizer

Is the SQL Server on the same box where you are running this query? If not, then the file is being created on the database server and not on your client machine. xp_cmdshell runs from the server. When you run bcp.exe from your own command prompt, it runs from the client.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/



Ahh.. good point, I bet that's it.



Future guru in the making.
Go to Top of Page

guna_dev
Starting Member

2 Posts

Posted - 2007-09-18 : 02:45:54
This is what i too get, but text file is not created
output
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
NULL
Starting copy...
NULL
9 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.): total 1
NULL

(7 row(s) affected)

could some one help me out.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-09-18 : 12:05:57
guna_dev, did you see my post?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-09-18 : 12:21:38
I just spit coffee all over my monitor



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -