在 SQL 存储过程中连接单引号

问题描述 投票:0回答:1

我正在开发一个分区自动化脚本,我需要如下的字符串连接输出。查询的前缀和后缀将被封装在单独的语句中

PARTITION pweek38 VALUES LESS THAN (TO_DAYS('2024-09-22 00:00:00.000000'))

下面是我的代码

SET @q = 'SELECT CONCAT(@query,'' PARTITION '', @partition_name, '' VALUES LESS THAN (TO_DAYS('', @partition_ts, '')'', '')'') INTO @query';

以上语句未在日期时间值之间添加单引号。

mysql stored-procedures mariadb
1个回答
0
投票
SET @q = 'SELECT CONCAT(@query,'' PARTITION '', @partition_name, '' VALUES LESS THAN (TO_DAYS('''''', @partition_ts, '''''')'', '')'') INTO @query';
SELECT @q;
SET @query = '', @partition_name = 'PARTITION_1', @partition_ts = CURRENT_DATE;
PREPARE stmt FROM @q;
EXECUTE stmt;
DROP PREPARE stmt;
SELECT @query;
@q
SELECT CONCAT(@query,'PARTITION',@partition_name,'值小于(TO_DAYS(''',@partition_ts,''')',')') INTO @query
@查询
PARTITION PARTITION_1 值小于 (TO_DAYS('2024-09-17'))

小提琴

© www.soinside.com 2019 - 2024. All rights reserved.