-1

我有查询。我想在 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
4

2 回答 2

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 ;
于 2012-09-21T06:48:02.977 回答
0

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

PRINT 也不是 MySQL 语句。

于 2012-09-21T07:11:41.140 回答