我正在尝试在mysql工作台中创建过程。但我无法修复程序中的错误。下面是我的表格和数据
创建表product(pid int主键,productname varchar(45))插入产品值(10,'a')插入产品值(12,'a')插入产品值(13,'a')
create table productdata(prid int primary key,prname varchar(45))insert into productdata values(1,'a')insert into productdata values(2,'b')insert into productdata values(3,'c')
创建表pr\u status(pesid int主键,pid int references product(pid),stage varchar(20),statusdate varchar(45))
插入pr\u状态值(1,10,'4','2018-05-23')插入pr\u状态值(4,10,'4','2018-05-23')插入pr\u状态值(2,12,'5','2018-05-24')插入pr\u状态值(3,13,'4','2018-05-25')
创建表工作(wid int主键、prid int references productdata(prid)、pid int references product(pid))
插入工作值(1,2,10)插入工作值(2,2,12)插入工作值(3,1,13)
选择从产品选择从产品数据选择从请购单状态选择从工作
我正在尝试使用这个过程从这个表中获取数据。
DROP PROCEDURE IF EXISTS test_mysql_while_loop$$
DROP TEMPORARY TABLE IF EXISTS esubmit$$
CREATE PROCEDURE test_mysql_while_loop()
BEGIN
DECLARE v_startdate VARCHAR(45), v_enddate VARCHAR(45)
SET v_startdate='2018-05-22'
set v_enddate='2018-05-28'
declare v_sql longtext
create temporary table #esubmit(id int auto_increment,Name varchar(100));
SET v_sql = 'ALTER TABLE #esubmit ADD ';
declare v_status int
declare v_count int
set v_status=0
set v_count=1
while(v_status!=1)
do
/* print cast(TIMESTAMPADD(day, v_count, cast(v_startdate as datetime(3))) as date) */
/* print cast(v_enddate as date) */
if(cast(TIMESTAMPADD(day, v_count, cast(v_startdate as datetime(3))) as date)=cast(v_enddate as date))
then
/* print v_sql */
set v_status=1;
set v_sql+=Concat('[',cast(TIMESTAMPADD(day,v_count, cast(v_startdate as datetime(3))) as varchar(45)),'] varchar(45)')
else
set v_sql+=Concat('[',cast(TIMESTAMPADD(day,v_count, cast(v_startdate as datetime(3))) as varchar(45)),'] varchar(45),')
end if;
set v_count=v_count+1
end while
set @stmt_str = v_sql;
prepare stmt from @stmt_str;
execute stmt;
deallocate prepare stmt;
declare v_cid int
declare v_eid int
declare v_setdate varchar(45)
c
fetch next from c into v_cid,v_eid
while(NOT_FOUND=0)
do
declare v_setcnt int
declare v_getdate varchar(45)
declare v_getname varchar(45)
open c1
fetch next from c1 into v_setdate
/* print v_setdate */
declare v_s int
set v_s=0
declare v_qry longtext
select prname into v_getname from productdata where prid=v_cid
/* print v_getname */
set v_qry=Concat('insert into #esubmit values(''',v_getname,''',')
/* print v_qry */
while(NOT_FOUND=0)
do
/* print CONVERT(CAST(v_setdate AS CHAR(24)), DATETIME) */
select count(pid) into v_setcnt from pr_status where pid=(select pid from work where pid=v_eid and prid=v_cid) and
cast(statusdate as date)=cast(v_setdate as date) and stage BETWEEN '4' AND '5' group by pid,statusdate
/* print v_setcnt */
select statusdate into v_getdate from pr_status where pid=(select pid from work where pid=v_eid and prid=v_cid) and
cast(statusdate as date) =cast(v_setdate as date) and stage BETWEEN '4' AND '5' group by pid,statusdate
set v_qry=v_qry+cast(v_setcnt as varchar(10)),','
/* print v_qry */
fetch next from c1 into v_setdate
set v_setcnt=0
end while
set v_qry=concat(substring(v_qry,0,char_length(rtrim(v_qry))),')')
set @stmt_str = v_qry;
prepare stmt from @stmt_str;
execute stmt;
deallocate prepare stmt;
set v_qry=''
deallocate c1
fetch next from c into v_cid,v_eid
end while
END
但我得到下面的错误代码:1064。sql语法有错误;请查看与您的mysql服务器版本对应的手册,以了解在第15行0.000秒处使用“begin if(cast(timestampadd(day,v\u count,cast(v\u startdate as datetime(3)))as da”的正确语法
暂无答案!
目前还没有任何答案,快来回答吧!