mysql存储过程运行报错,一直提示1064,在存储过程声明变量的时候就提示错,动态条件查询的sql 15
这是存储过程,in后面的参数都是后台需要封装的条件;DELIMITER$$CREATE/*[DEFINER={user|CURRENT_USER}]*/PROCEDURE...
这是存储过程,in后面的参数都是后台需要封装的条件;
DELIMITER $$
CREATE
/*[DEFINER = { user | CURRENT_USER }]*/
PROCEDURE `selectCollectdata`(IN ctype VARCHAR(50),IN score INT,IN pageNum INT,IN pageSize INT,IN orders VARCHAR(5),IN title VARCHAR(255),
IN content VARCHAR(200)IN ,keyWords VARCHAR(200),IN startTime VARCHAR(20),IN endTime VARCHAR(20),IN taskId VARCHAR(30))
/*LANGUAGE SQL
`insertCollectData` | [NOT] DETERMINISTIC
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
| COMMENT 'string'*/
BEGIN
DECLARE @sql VARCHAR(MAX);
SET @sql= 'select c.id cid,c.score,c.publishDate,c.author,c.title,c.content,c.source,c.url,c.source,c.click,c.zanTotal,
c.repeatTotal,c.forward,c.type,t.key_words,t.id tId
from t_collectdata c
LEFT JOIN t_data_subject d on d.id=c.id
LEFT JOIN t_task t on d.subjectId=t.id where 1=1 '
BEGIN
IF(ctype IS NOT NULL)
SET @sql =@sql + 'and c.type='TYPE' '
IF(startTime IS NOT NULL)
SET @sql =@sql + 'and c.publishDate between 'endTime' and 'startTime' '
IF(taskId IS NOT NULL)
SET @sql =@sql + 'and t.id in(taskId)'
IF(keyWords IS NOT NULL)
SET @sql =@sql + 'and (c.title like ''%'+title+'%'' union all c.content like ''%'+content+'%'')'
IF(title IS NOT NULL)
SET @sql =@sql + 'and c.title like ''%'+title+'%'''
IF(content IS NOT NULL)
SET @sql =@sql + 'and c.content like ''%'+content+'%'''
IF(score ==1)
SET @sql =@sql +'and 0.55<c.score<=1'
IF(score ==2)
SET @sql =@sql +'and 0.45<=c.score<=0.55'
IF(score ==-1)
SET @sql =@sql +'and 0<=c.score<0.45'
IF(orders IS NOT NULL)
SET @sql =@sql +'c.publishDate order by 'orders''
IF(pageSize>0)
SET @sql =@sql +'limit 'pageNum','pageSize''
END IF;
exec(@sql)
END $$`
DELIMITER ;
系统提示时运行报错,求大腿帮忙。看下是不是存储过程就写错了,还是说动态的多条件查询拼接不能这样写。 展开
DELIMITER $$
CREATE
/*[DEFINER = { user | CURRENT_USER }]*/
PROCEDURE `selectCollectdata`(IN ctype VARCHAR(50),IN score INT,IN pageNum INT,IN pageSize INT,IN orders VARCHAR(5),IN title VARCHAR(255),
IN content VARCHAR(200)IN ,keyWords VARCHAR(200),IN startTime VARCHAR(20),IN endTime VARCHAR(20),IN taskId VARCHAR(30))
/*LANGUAGE SQL
`insertCollectData` | [NOT] DETERMINISTIC
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
| COMMENT 'string'*/
BEGIN
DECLARE @sql VARCHAR(MAX);
SET @sql= 'select c.id cid,c.score,c.publishDate,c.author,c.title,c.content,c.source,c.url,c.source,c.click,c.zanTotal,
c.repeatTotal,c.forward,c.type,t.key_words,t.id tId
from t_collectdata c
LEFT JOIN t_data_subject d on d.id=c.id
LEFT JOIN t_task t on d.subjectId=t.id where 1=1 '
BEGIN
IF(ctype IS NOT NULL)
SET @sql =@sql + 'and c.type='TYPE' '
IF(startTime IS NOT NULL)
SET @sql =@sql + 'and c.publishDate between 'endTime' and 'startTime' '
IF(taskId IS NOT NULL)
SET @sql =@sql + 'and t.id in(taskId)'
IF(keyWords IS NOT NULL)
SET @sql =@sql + 'and (c.title like ''%'+title+'%'' union all c.content like ''%'+content+'%'')'
IF(title IS NOT NULL)
SET @sql =@sql + 'and c.title like ''%'+title+'%'''
IF(content IS NOT NULL)
SET @sql =@sql + 'and c.content like ''%'+content+'%'''
IF(score ==1)
SET @sql =@sql +'and 0.55<c.score<=1'
IF(score ==2)
SET @sql =@sql +'and 0.45<=c.score<=0.55'
IF(score ==-1)
SET @sql =@sql +'and 0<=c.score<0.45'
IF(orders IS NOT NULL)
SET @sql =@sql +'c.publishDate order by 'orders''
IF(pageSize>0)
SET @sql =@sql +'limit 'pageNum','pageSize''
END IF;
exec(@sql)
END $$`
DELIMITER ;
系统提示时运行报错,求大腿帮忙。看下是不是存储过程就写错了,还是说动态的多条件查询拼接不能这样写。 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询