Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL BCP Script

Posted on 2013-10-23
3
Medium Priority
?
839 Views
Last Modified: 2014-01-16
I'm using the following script and get an error saying " > was expected at this point"  and I can't figure out what's wrong with it ...  Variables are defined at the top of the batch file.

Thanks a million for any help ..

bcp "SELECT DISTINCT PM.vwGenPatInfo.Patient_Last_Name, PM.vwGenPatInfo.Patient_First_Name, PM.vwGenPatInfo.Patient_MI, PM.vwGenPatInfo.Patient_DOB, PM.vwGenPatInfo.Patient_Sex,

PM.vwGenPatInfo.Patient_Street1, PM.vwGenPatInfo.Patient_Street2,PM.vwGenPatInfo.Patient_City, PM.vwGenPatInfo.Patient_State, PM.vwGenPatInfo.Patient_Zip_Code,

PM.vwGenPatInfo.Patient_Home_Phone,PM.vwGenPatInfo.Patient_SSN, PM.vwApptDetail.Appointment_DateTime FROM %DBNAME%.PM.vwApptDetail INNER JOIN %DBNAME%.PM.vwGenPatInfo ON

PM.vwApptDetail.Patient_Number = PM.vwGenPatInfo.Patient_Number
WHERE (%DBNAME%.PM.vwApptDetail.Appointment_DateTime BETWEEN CONVERT(date, DATEADD(day, 1, GETDATE())) AND CONVERT(date, DATEADD(day, 4, GETDATE()))) AND (%DBNAME

%.PM.vwApptDetail.Status <> 'X') AND (%DBNAME%.PM.vwApptDetail.Patient_Number <> '300780')" queryout %CSVPATH%\appt.csv -S%SERVERNAME% -c -t^| -U%SQLLOGIN% -P%SQLPASSWORD%
0
Comment
Question by:jtbrown1111
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 27

Accepted Solution

by:
Chris Luttrell earned 2000 total points
ID: 39596346
I get the error "> was unexpected at this time." if I have the quoted sql command split on more than one line.  Is that what is happening to you?  You can continue the command with the carrot ^ anywhere a space is allowed, but not inside the quoted string.
HTH, Chris
0
 
LVL 11

Expert Comment

by:John_Vidmar
ID: 39706018
In your example, if DBNAME contains trailing white-space (space, tab, newline) then that will mess up your dynamic-SQL.  Instead of building the SQL dynamically with command-script variable for database, use the -d option (I would also place the SQL in a file and use -i option).
usage: bcp {dbtable | query} {in | out | queryout | format} datafile
  [-m maxerrors]            [-f formatfile]          [-e errfile]
  [-F firstrow]             [-L lastrow]             [-b batchsize]
  [-n native type]          [-c character type]      [-w wide character type]
  [-N keep non-text native] [-V file format version] [-q quoted identifier]
  [-C code page specifier]  [-t field terminator]    [-r row terminator]
  [-i inputfile]            [-o outfile]             [-a packetsize]
  [-S server name]          [-U username]            [-P password]
  [-T trusted connection]   [-v version]             [-R regional enable]
  [-k keep null values]     [-E keep identity values]
  [-h "load hints"]         [-x generate xml format file]
  [-d database name]

Open in new window

0
 
LVL 27

Expert Comment

by:Zberteoc
ID: 39759420
Make sure your bcp command is on one line entirely. Also you can use != instead <>, just in case:

bcp "SELECT DISTINCT PM.vwGenPatInfo.Patient_Last_Name, PM.vwGenPatInfo.Patient_First_Name, PM.vwGenPatInfo.Patient_MI, PM.vwGenPatInfo.Patient_DOB, PM.vwGenPatInfo.Patient_Sex, PM.vwGenPatInfo.Patient_Street1, PM.vwGenPatInfo.Patient_Street2,PM.vwGenPatInfo.Patient_City, PM.vwGenPatInfo.Patient_State, PM.vwGenPatInfo.Patient_Zip_Code, PM.vwGenPatInfo.Patient_Home_Phone,PM.vwGenPatInfo.Patient_SSN, PM.vwApptDetail.Appointment_DateTime FROM %DBNAME%.PM.vwApptDetail INNER JOIN %DBNAME%.PM.vwGenPatInfo ON PM.vwApptDetail.Patient_Number = PM.vwGenPatInfo.Patient_Number WHERE (%DBNAME%.PM.vwApptDetail.Appointment_DateTime BETWEEN CONVERT(date, DATEADD(day, 1, GETDATE())) AND CONVERT(date, DATEADD(day, 4, GETDATE()))) AND (%DBNAME%.PM.vwApptDetail.Status <> 'X') AND (%DBNAME%.PM.vwApptDetail.Patient_Number != '300780')" queryout %CSVPATH%\appt.csv -S%SERVERNAME% -c -t^| -U%SQLLOGIN% -P%SQLPASSWORD%

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question