My-sql查询中的DECLARE错误

时间:2012-09-21 06:35:28

标签: mysql phpmyadmin

我正在查询。我想在phpmyadmin上运行。

但每当我跑步时,我都会在申报时遇到错误。 我无法找出这里的错误是什么?

我想将此代码块作为PHP的查询运行吗?

-- Declare a cursor to hold the section information for a specified product
DECLARE section_cursor CURSOR FOR
SELECT DISTINCT cds_especee.sectid, cds_evocee.text, min
(displayorder) displayorder
FROM cds_especee
JOIN cds_evocee ON (cds_especee.sectid = cds_evocee.id)
-- Replace the prodid below with a valid prodid from your data set
WHERE cds_especee.prodid = 'S1522339'
GROUP BY cds_especee.sectid, cds_evocee.text
ORDER BY displayorder ASC
OPEN section_cursor
-- Fetching the first section into variable
FETCH NEXT FROM section_cursor
INTO @section, @sectionname, @order
WHILE @@FETCH_STATUS = 0
BEGIN
-- Print current section to the screen
PRINT '******'+@sectionname+'******'
-- Declare a cursor to hold the header and body information for the
specified product
-- Note that the WHERE statement limits the results to the headers and body falling under the current section
DECLARE espec_cursor CURSOR FOR
SELECT evoc1.text, evoc2.text, displayorder
FROM cds_especee
JOIN cds_evocee evoc1 ON (cds_especee.hdrid = evoc1.id)
JOIN cds_evocee evoc2 ON (cds_especee.bodyid = evoc2.id)
-- Replace the prodid below with a valid prodid from your data set
WHERE cds_especee.prodid = 'S1522339' AND
cds_especee.sectid = @section
ORDER BY displayorder ASC
OPEN espec_cursor
-- Fetching the first header and body into variable
FETCH NEXT FROM espec_cursor
INTO @hdr, @body, @order1
WHILE @@FETCH_STATUS = 0
-- Below is a loop that prints all the headers and bodies for the
current section
BEGIN
PRINT @hdr +': '+@body
FETCH NEXT FROM espec_cursor
INTO @hdr, @body, @order1
END
-- Clear out the espec_cursor as it will be repopulated with data for
the next section as the procedure loops
CLOSE espec_cursor
DEALLOCATE espec_cursor
-- Fetches the next section and returns to the top of the loop
FETCH NEXT FROM section_cursor
INTO @section, @sectionname, @order
PRINT ' '
PRINT ' '
END
-- Clear out the section_cursor as the procedure is over
CLOSE section_cursor
DEALLOCATE section_cursor

2 个答案:

答案 0 :(得分:0)

也许是关于mysql分隔符的东西。尝试像这样运行它:

DELIMITER $$
-- Declare a cursor to hold the section information for a specified product 
DECLARE section_cursor CURSOR FOR 
SELECT DISTINCT cds_especee.sectid, cds_evocee.text, min 
(displayorder) displayorder 
FROM cds_especee 
JOIN cds_evocee ON (cds_especee.sectid = cds_evocee.id) 
-- Replace the prodid below with a valid prodid from your data set 
WHERE cds_especee.prodid = 'S1522339' 
GROUP BY cds_especee.sectid, cds_evocee.text 
ORDER BY displayorder ASC 
OPEN section_cursor 
-- Fetching the first section into variable 
FETCH NEXT FROM section_cursor 
INTO @section, @sectionname, @order 
WHILE @@FETCH_STATUS = 0 
BEGIN 
-- Print current section to the screen 
PRINT '******'+@sectionname+'******' 
-- Declare a cursor to hold the header and body information for the 
specified product 
-- Note that the WHERE statement limits the results to the headers and body falling under the current section 
DECLARE espec_cursor CURSOR FOR 
SELECT evoc1.text, evoc2.text, displayorder 
FROM cds_especee 
JOIN cds_evocee evoc1 ON (cds_especee.hdrid = evoc1.id) 
JOIN cds_evocee evoc2 ON (cds_especee.bodyid = evoc2.id) 
-- Replace the prodid below with a valid prodid from your data set 
WHERE cds_especee.prodid = 'S1522339' AND 
cds_especee.sectid = @section 
ORDER BY displayorder ASC 
OPEN espec_cursor 
-- Fetching the first header and body into variable 
FETCH NEXT FROM espec_cursor 
INTO @hdr, @body, @order1 
WHILE @@FETCH_STATUS = 0 
-- Below is a loop that prints all the headers and bodies for the 
current section 
BEGIN 
PRINT @hdr +': '+@body 
FETCH NEXT FROM espec_cursor 
INTO @hdr, @body, @order1 
END 
-- Clear out the espec_cursor as it will be repopulated with data for 
the next section as the procedure loops 
CLOSE espec_cursor 
DEALLOCATE espec_cursor 
-- Fetches the next section and returns to the top of the loop 
FETCH NEXT FROM section_cursor 
INTO @section, @sectionname, @order 
PRINT ' ' 
PRINT ' ' 
END 
-- Clear out the section_cursor as the procedure is over 
CLOSE section_cursor 
DEALLOCATE section_cursor$$ 
DELIMITER ;

答案 1 :(得分:0)

您不能在简单的MySQL脚本中使用许多语句,包括DECLARE,OPEN,FETCH,CLOSE。您应该为此目的创建一个源对象。例如,您可以使用存储过程来完成它。文档中有一个很好的例子 - Cursors

PRINT也不是MySQL语句。