Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BCP with query

Status
Not open for further replies.

frangac

Technical User
Feb 8, 2004
163
ZA
Hi all,

Could anyone please assist me with the following cmd

Select "bcp " + db_name()
+ ".." + name +
" out c:\temp\" + name + ".txt /c -t\t -SserverName
-UuserID -Ppassword /b1000"
from sysobjects
where type = "U"
order by name

I am trying to run the below but with errors.

Select "bcp " + audit_db()
+ ".." + FILE_AUDIT +
" out FILE_AUDIT_040715".txt -SCHRIS_D
-Usa -Pchris"
from audit_db..FILE_AUDIT
where EVENT_DT < '2004/02/01'


Many Thanks
Chris



 
Depending on how your server is set up, your errors may be a result of using double quote " instead of single quote '

I guess you are trying to produce a list of lines that will become bcp commands to run from a windows cmd prompt?

Chris
 
Hi Chris,

The error mesg is 'audit_db' is not a recognized built-in function name.
So how would your cmd be?

Thanks
Chris
 
If audit_db is the name of a database in which you keep audit info, then I think you want
Code:
Select 'bcp audit_db..FILE_AUDIT ' +
' out FILE_AUDIT_040715.txt -SCHRIS_D -Usa -Pchris ' +
' from audit_db..FILE_AUDIT' +
' from sysobjects ' +
' where type = ''U'' ' +
' order by name '
 
Sorry, mistake there;
Code:
Select 'bcp audit_db..FILE_AUDIT ' +
' out FILE_AUDIT_040715.txt -SCHRIS_D -Usa -Pchris ' +
' from sysobjects ' +
' where type = ''U'' order by name '
is what you're original SQL snippet was trying to do - generate a list of bcp commands that can be run from Windows.
On the other hand
Code:
select * from audit_db..FILE_AUDIT
where EVENT_DT < '20040201'
might be the SQL code that will list all your events in FILE_AUDIT table before 2004/02/01

 
Hi Chris,

Thanks for your input, but no output file?????

1> Select 'bcp audit_db..FILE_AUDIT ' + ' out FILE_AUDIT_040715.txt SCHRIS_D -Usa -Pchris ' + ' from sysobjects ' + ' where audit_db..FILE_AUDIT.EVENT_DT < "2004/05/01"'
2> go

If I select where audit_db..FILE_AUDIT.EVENT_DT < "2004/05/01", it returns with 200 rows

Thanks Again
Chris


-----------------------------------------------------------------------------------------------------------------------------------------------------

bcp audit_db..FILE_AUDIT out FILE_AUDIT_040715.txt -SSYB_DRMS_TRG -Usa -Puiop12 from sysobjects where audit_db..FILE_AUDIT.EVENT_DT < "2004/05/01"

(1 row affected)
1>
 
Chris,
Do you know what you are trying to achieve?
If so, can you explain what it is.
Chris
 
Chris,

I am "trying" to bcp out from a select statement. The reason for this is that the table contains a lot of data and it is not possible to bcp out the whole table.

Thanks
Chris
 
Gotcha.
The original select at the top of your post is an SQL statement designed to generate lots of lines beginning with "bcp ..." which can then be cut and paste into a windows command line or DOS shell

Since you only want to copy from 1 table, you don't need to use the select to generate your bcp command. You can just type it at the windows command

So: open a dos window.
type your bcp command :
Code:
bcp "select * from audit_db..FILE_AUDIT where EVENT_DT < '2004/05/01'" out FILE_AUDIT_040715.txt 
-SSYB_DRMS_TRG -Usa -Puiop12

if you get error messages, try
Code:
bcp /?
for help, and take it from there.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top