mysql 存储过程中的 concat() long siring

concate() long siring in mysql store procedure

我正在尝试执行动态查询,因为我正在使用 CONCAT() 函数。但我认为我的 string/query 太久了,所以 CONCAT() 无法正常工作。

set @query:=CONCAT(' SELECT (select GROUP_CONCAT(sp.specialization) FROM DSpecialization_Master dsp LEFT JOIN 
Specialization_Master sp on sp.id = dsp.specialization WHERE dsp.profileid in (', @MultiDoctorIds ,') 
) as drspec,pm.id as profileid,dam.city,um.profile_url FROM Profile_Master pm LEFT JOIN DAddress_Master dam on 
dam.profileid = pm.id join Unique_Url_Master um on um.clinicid =dam.id WHERE pm.id in (',@MultiDoctorIds,') 
and dam.id=',@clinicId,'  order by CASE WHEN um.clinic_url=''',@ClinicUrl,''' THEN 1 ELSE 2  end,um.clinic_url   ');

select @query;

这是存储过程的一部分,当我执行我的 SP 时它会显示 @query NULL 但是如果我缩短我的查询,那么我会得到完整的查询。

还有其他连接方式吗??

感谢任何帮助。

A temporary solution 为此,将您的 dynamic query 存储在 local variable 而不是 sessional variablemaximum length 中。如下所示

Declare temp_query varchar(4000);

set @query:=CONCAT(' SELECT (select GROUP_CONCAT(sp.specialization) FROM DSpecialization_Master dsp LEFT JOIN 
Specialization_Master sp on sp.id = dsp.specialization WHERE dsp.profileid in (', @MultiDoctorIds ,') 
) as drspec,pm.id as profileid,dam.city,um.profile_url FROM Profile_Master pm LEFT JOIN DAddress_Master dam on 
dam.profileid = pm.id join Unique_Url_Master um on um.clinicid =dam.id WHERE pm.id in (',@MultiDoctorIds,') 
and dam.id=',@clinicId,'  order by CASE WHEN um.clinic_url=''',@ClinicUrl,''' THEN 1 ELSE 2  end,um.clinic_url   ');

select @query into temp_query;
select temp_query;

你可以像这样设置一个变量

SET @var := hello hello

必须引用

MariaDB [(none)]> set @var = hello hello;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'hello' at line 1
MariaDB [(none)]> set @var = "hello hello";
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select @var;
+-------------+
| @var        |
+-------------+
| hello hello |
+-------------+
1 row in set (0.00 sec)

MariaDB [(none)]>

我得到了另一个解决方案:

GROUP_CONCAT 最大长度是限制 (1024 by default) 你应该像下面那样改变它的会话范围长度,这将解决你的问题

  SET SESSION group_concat_max_len = 25000 ;