concate()在mysql存储过程中长时间连接

时间:2016-04-07 11:31:31

标签: mysql stored-procedures concatenation dynamic-queries

我尝试为我使用CONCAT()函数执行动态查询。但我认为我的字符串/查询很长,所以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 ,但如果我缩短查询,那么我将获得完整的查询。

还有另一种连接方法吗?

任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:1)

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;

答案 1 :(得分:0)

您可以设置类似

的变量
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)]>

答案 2 :(得分:0)

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

GROUP_CONCAT最大长度是限制(1024 by default)你应该改变它的会话范围长度,如下所示将解决你的麻烦

  SET SESSION group_concat_max_len = 25000 ;