如何在SELECT CONCAT

时间:2019-03-04 00:24:38

标签: mysql string concat character-replacement

我需要将下面转换的“ DebitAmount”和“ CreditAmount”字段导出到“ OUTFILE”,但是我想因为它们是SELECT CONCAT内的SELECT的一部分,所以我无法直接替换,这会导致工作台信号严重偏离语法错误:“缺少右括号”。我该如何正确使用这些功能?

CREATE DEFINER=`root`@`localhost` PROCEDURE `p_test`(in_Accountid 
varchar(30))
BEGIN

SET @default_group_concat_max_len = (SELECT @@group_concat_max_len);
SET SESSION group_concat_max_len = 1000000;
SET @FilePath:='C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/';
SET @FileName:=CONCAT(in_AccountID,'_',DATE_FORMAT(NOW(),'%Y-%m- 
%d'),'.xls');
SET @FullFilePath:=CONCAT(@FilePath,@FileName);
SET @TableName:='temp_transactionslines';
SET @TableSchema:='melhor_metade_2016';


SET @SQL = ( select CONCAT('SELECT  
TransactionID
,RecordID
,AccountID
,AccountDescription
,SourceDocumentID
,SystemEntryDate
,Description
--,DebitAmount                         
,replace(ifnull(round(DebitAmount,2),'0.00'),'.',',')
--,CreditAmount                                                 , 
,replace(ifnull(round(CreditAmount,2),'0.00'),'.',',')
,Period
,TransactionDate
,CustomerTaxID
,CustomerID 
INTO OUTFILE \'', @FullFilePath, '\'
FROM (SELECT '
,group_concat(CONCAT("'", COLUMN_NAME,"'"))
,' UNION ALL (SELECT * FROM temp_transactionslines  order by TransactionID 
 ASC)) as tmp')

from INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = @TableName
AND TABLE_SCHEMA = @TableSchema
order BY ORDINAL_POSITION );

SET SESSION group_concat_max_len = @default_group_concat_max_len;

PREPARE dynamic_statement FROM @SQL;
EXECUTE dynamic_statement;
DEALLOCATE PREPARE dynamic_statement;

END

0 个答案:

没有答案