Solved

BCP Using a variable in the Where Clause

Posted on 2014-01-17
1
1,130 Views
Last Modified: 2014-01-17
I need to pass a variable to bcp using queryout

This is being called from within a stored procedure like this:

While @minpo < @maxpo
Begin

SET @CmdString = 'bcp "SELECT * FROM #Outputtmp where ponumber = "" @minpo """ queryout "' + @Directory + '\' + @FileName + '" -c -U' + @User + ' -P' + @Pass


SELECT TOP 1 @minPO = PONumber
FROM #outputtmp
WHERE PONumber > @minpo
ORDER BY PONumber

End

@CmdString ends up looking like this:

bcp "SELECT * FROM #Outputtmp where ponumber = "" @minpo """ queryout "c:\570\101370000570.oro" -c -Usoandso -PN/A

Can anyone help me with this.  

Thanks.
0
Comment
Question by:sherbug1015
1 Comment
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39788688
this should work better:
SET @CmdString = 'bcp "SELECT * FROM #Outputtmp where ponumber = ''' + @minpo + ''' queryout "' + @Directory + '\' + @FileName + '" -c -U' + @User + ' -P' + @Pass

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

770 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