SQL 中 bcp 命令的查询语法有问题
Something is wrong with a query syntax for bcp command in SQL
我正在为 SQL 中的 bcp 命令构建查询。
执行语句时出现以下错误:
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.
这是我的 bcp 命令的语法:
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
我不确定哪里出了问题。
执行时,打印以下查询:
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'
有时我一直在尝试解决它,但它真的困扰着我。
你能解释一下我做错了什么吗?如果可能的话,请告诉我正确的方法吗?
这是因为查询 'SELECT ''Transaction...' 是 varchar 并且输入表示为 INT 尽管它是 varchar。使用 :
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 = ''' + CONVERT(VARCHAR, @input) + ''''
您的数据类型不匹配。您查询末尾的 + @input
是问题所在。
我猜 [YYYYMM]
列是一个数字?应该是 string/varchar.
所以你的台词可以是
declare @input as varchar
set @input = '201505'
set @query = 'SELECT ''Transaction Unique ID'',''Transaction Date''...WHERE YYYYMM = ''' + @input + ''''
如果 [YYYYMM]
是一个字符串,或者如果它不是并且它是数字
declare @input as int
set @input = 201505
set @query = 'SELECT ''Transaction Unique ID'',''Transaction Date''...WHERE YYYYMM = ' + cast(@input as varchar)
我解决了这个问题。问题是我没有设置用于构建文件名的变量。 bcp 执行时,文件名没有正确建立
我正在为 SQL 中的 bcp 命令构建查询。 执行语句时出现以下错误:
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.
这是我的 bcp 命令的语法:
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
我不确定哪里出了问题。
执行时,打印以下查询:
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'
有时我一直在尝试解决它,但它真的困扰着我。
你能解释一下我做错了什么吗?如果可能的话,请告诉我正确的方法吗?
这是因为查询 'SELECT ''Transaction...' 是 varchar 并且输入表示为 INT 尽管它是 varchar。使用 :
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 = ''' + CONVERT(VARCHAR, @input) + ''''
您的数据类型不匹配。您查询末尾的 + @input
是问题所在。
我猜 [YYYYMM]
列是一个数字?应该是 string/varchar.
所以你的台词可以是
declare @input as varchar
set @input = '201505'
set @query = 'SELECT ''Transaction Unique ID'',''Transaction Date''...WHERE YYYYMM = ''' + @input + ''''
如果 [YYYYMM]
是一个字符串,或者如果它不是并且它是数字
declare @input as int
set @input = 201505
set @query = 'SELECT ''Transaction Unique ID'',''Transaction Date''...WHERE YYYYMM = ' + cast(@input as varchar)
我解决了这个问题。问题是我没有设置用于构建文件名的变量。 bcp 执行时,文件名没有正确建立