我想使用动态SQL简化以下操作,就像在Transact SQL中一样。
我想做类似的事情:
SET @s = replace(field_name, '_complete','')
并使用@s代替replace(field_name,'_ complete','')
如果可能请尽情,如果可以的话。
我目前的代码:
select distinct
if(instr(replace(field_name, '_complete',''),'_') <= 5
,left(replace(field_name, '_complete','')
,instr(replace(field_name, '_complete',''),'_') - 1
)
,replace(field_name, '_complete','')
) AS form_id ,replace(
if(instr(replace(field_name, '_complete',''),'_') <= 5,
mid(replace(field_name, '_complete',''),
instr(replace(field_name, '_complete',''),'_') + 1,
length(replace(field_name, '_complete','')) - instr(replace(field_name, '_complete',''),'_')
)
,replace(field_name, '_complete','')
),
'_',
' ') as form_name ,field_name from redcap_extract2use where field_name like '%_complete' order by 1;
然后将上面的内容替换为:
select distinct
if(instr(@s,'_') <= 5 ,left(@s,instr(@s,'_') - 1),@s ) AS form_id
,replace( if(instr(@s,'_') <= 5,
mid(@s,instr(@s,'_') + 1,length(@s) - instr(@s,'_')),@s), '_', ' ') as form_name
,field_name
from redcap_extract2use
where field_name like '%_complete'
order by 1;
我会有一个执行...来运行查询