SQL Query to text file "WHERE" syntax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • James Kessler
    New Member
    • Nov 2011
    • 1

    SQL Query to text file "WHERE" syntax

    Hey guys,

    I know there have been plenty of posts on this topic. I did my research and have my Query outputting to a text file as I wanted. However I have a question with adding something to my command.

    Here is my command that is working:

    EXEC XP_CmdShell 'BCP "Select code, desc From DATABASE.dbo.ta ble" queryout "C:\test.tx t" -c -T'



    I want to add a "WHERE" command to this line, where I have a value equal to something...Exa mple:

    WHERE mat = '8888'



    I know I need to declare "mat" as part of a different table in the database. Here is what I have at the moment...

    EXEC XP_CmdShell 'BCP "Select code, desc From DATABASE.dbo.ta ble, DATABASE.dbo.Ot herTable b WHERE b.mat = '8888'" queryout "C:\test.tx t" -c -T'

    I am getting a syntax error where '8888' is. I know it is because of the ' character that is commenting out my equal to value. What is the correct syntax to do this? I have tried many things and still haven't gotten it to work. Any help is appreciated! Thanks everyone!
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Instead of one single quote before and after the 8888, use two single quote. Lave the double-quote to enclose the query.

    Happy Coding!!!


    ~~ CK

    Comment

    Working...