[Solved] Something is wrong with a query syntax for bcp command in SQL

EverSQL Database Performance Knowledge Base

Something is wrong with a query syntax for bcp command in SQL

I'm building a query for my bcp command in SQL. When executing the statement I have the following error:

Conversion failed when converting the varchar value 'SELECT 'Transaction Unique ID','Transaction Date', 'Person Unique ID' UNION ALL SELECT NULL AS 'Transaction Unique ID', CONVERT(VARCHAR(10),CONVERT(DATETIME,Date,1),101) AS ' Transaction Date', NULL AS 'Person Unique ID' FROM tblChromeRiverInitData WHERE YYYYMM = ' to data type int.

This is the syntax for my bcp command:

DECLARE @query VARCHAR(2000)
DECLARE @bcpCommand VARCHAR(1024)
DECLARE @sharedDevFolder VARCHAR(500)
DECLARE @fileName VARCHAR(200)
DECLARE @environment VARCHAR(5)
DECLARE @customerCode VARCHAR(5)
DECLARE @parserConfig VARCHAR(5)
DECLARE @bucketAssign VARCHAR(10)
DECLARE @dateFormat VARCHAR(15)
DECLARE @input VARCHAR(15)
DECLARE @RC int

SET @sharedDevFolder = '\\REMOTE_SERVER\MyDirectory\'
SET @input = 201507
SET @fileName = 'Transaction-' + 
                @environment + '-' + 
                @customerCode + '-' + 
                @parserConfig + '-' + 
                @bucketAssign + '-' + 
                @dateFormat + '.txt'

SET @query = 
    'SELECT ''Transaction Unique ID'',''Transaction Date'', ''Person Unique ID'' UNION ALL SELECT NULL AS ''Transaction Unique ID'', CONVERT(VARCHAR(10),CONVERT(DATETIME,Date,1),101) AS '' Transaction Date'', NULL AS ''Person Unique ID'' FROM tblChromeRiverInitData WHERE YYYYMM = ' + CAST(@input as VARCHAR(10))

SET @bcpCommand = 'bcp "' + @query + '" queryout "'
set @bcpCommand = @bcpCommand + @sharedDevFolder + @fileName + '" -c  -T -t^| -r\n'

EXEC @RC = master..xp_cmdshell @bcpCommand

I'm not sure what's wrong.

When executed, the following query is printed:

SELECT 'Transaction Unique ID','Transaction Date', 'Person Unique ID' UNION ALL SELECT NULL AS 'Transaction Unique ID', CONVERT(VARCHAR(10),CONVERT(DATETIME,Date,1),101) AS 'Transaction Date', NULL AS 'Person Unique ID' FROM tblChromeRiverInitData WHERE YYYYMM = '201507'

I have been trying to solve it for sometimes already and it's really bugging me.

Can you please explain what I'm doing wrong and if possible provide me with the correct way of doing this?

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  2. Use Numeric Column Types For Numeric Values (query line: 17): Referencing a numeric value (e.g. 201507) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
Optimal indexes for this query:
ALTER TABLE `tblChromeRiverInitData` ADD INDEX `tblchromeriverinit_idx_yyyymm` (`YYYYMM`);
The optimized query:
SELECT
        'Transaction Unique ID',
        'Transaction Date',
        'Person Unique ID' 
    UNION
    ALL SELECT
        NULL AS 'Transaction Unique ID',
        CONVERT(VARCHAR(10),
        CONVERT(DATETIME,
        tblChromeRiverInitData.Date,
        1),
        101) AS 'Transaction Date',
        NULL AS 'Person Unique ID' 
    FROM
        tblChromeRiverInitData 
    WHERE
        tblChromeRiverInitData.YYYYMM = '201507'

Related Articles



* original question posted on StackOverflow here.