光标"从"中取出 - 附近的语法错误

时间:2017-01-29 13:51:54

标签: sql cursor sybase procedure

我想创建一个程序,使用光标向我显示20%折扣的产品。编译完下面的代码后,我收到错误消息 "" FROM"附近的语法错误第14行。我使用的是Sybase。谁能帮我这个? 这是我的代码:

CREATE  PROCEDURE "ProduktyPoZnizce20procent2"( /* @parameter_name parameter_type [= default_value] [OUTPUT], ... */ )
AS
BEGIN
    declare @IDPR INTEGER 
    declare @typ  VARCHAR(30)
    declare @model varchar(30)
    declare @cena   float

DECLARE ProductCursor CURSOR FOR
SELECT IDProduct from Product

Open ProductCursor

FETCH NEXT FROM ProductCursor
INTO @IDPR

while(@@FETCH_STATUS = 0)
    BEGIN 
        select @typ = Product.product_type, @model=Product.name, @cena = Product.price * 0.8
        from Product
        where @IDPR = Product.IDProduct

        print(cast(@typ as nvarchar(30)) + ' ' + cast(@model as nvarchar(30)) + ' = ' + cast(@cena as varchar(10)))

        FETCH NEXT FROM ProductCursor INTO @IDPR

    END

close ProductCursor
DEALLOCATE CURSOR ProductCursor

END

2 个答案:

答案 0 :(得分:0)

你要做的就是

SELECT Cast(Product.product_type AS NVARCHAR(30))
       + " " + Cast(Product.NAME AS NVARCHAR(30))
       + " = "
       + Cast(Product.price * 0.8 AS VARCHAR(10))
FROM   Product 

在对sysbase中的cursor进行一些研究之后。我想

FETCH NEXT FROM ProductCursor INTO @IDPR

应该是

fetch ProductCursor into @IDPR
DEALLOCATE ProductCursor

应该是

deallocate cursor ProductCursor

要检查循环@@FETCH_STATUS中的记录,应为@@sqlstatus

/* now loop, processing all the rows
** @@sqlstatus = 0 means successful fetch
** @@sqlstatus = 1 means error on previous fetch
** @@sqlstatus = 2 means end of result set reached
*/

while (@@sqlstatus = 0)
Begin
..
End

点击此处查看有关sysbase http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.dc20020_1251/html/databases/X61512.htm

CURSOR的更多信息

答案 1 :(得分:0)

行。我的代码中有2个错误:我没有用光标前进。将光标发送到下一行的正确语法是

FETCH NEXT ProductCursor
INTO @IDPR

它应该在before循环和循环中调用。

接下来要在屏幕上正确打印结果。由于

print(cast(@typ as nvarchar(30)) + ' ' + cast(@model as nvarchar(30)) + ' = ' + cast(@cena as varchar(10)))

在屏幕上没有任何内容,必须用

代替
message (cast(@typ as nvarchar(30)) + ' ' + cast(@model as nvarchar(30)) + ' = ' + cast(@cena as varchar(10))) type status to client

整个代码现在看起来像这样:

CREATE  PROCEDURE "ProduktyPoZnizce20procent2"( /* @parameter_name parameter_type [= default_value] [OUTPUT], ... */ )
AS
BEGIN
    declare @IDPR INTEGER 
    declare @typ  VARCHAR(30)
    declare @model varchar(30)
    declare @cena   float

DECLARE ProductCursor CURSOR FOR
SELECT IDProduct from Product

Open ProductCursor

FETCH NEXT ProductCursor
INTO @IDPR

while(@@FETCH_STATUS = 0)
    BEGIN 
        select @typ = Product.product_type, @model=Product.name, @cena = Product.price * 0.8
        from Product
        where @IDPR = Product.IDProduct

        message(cast(@typ as nvarchar(30)) + ' ' + cast(@model as nvarchar(30)) + ' = ' + cast(@cena as varchar(10))) type status to client


    FETCH NEXT ProductCursor
    INTO @IDPR

    END

close ProductCursor
DEALLOCATE CURSOR ProductCursor

END