Sybase ASE:如何使用光标打印所有表行?

问题描述 投票:0回答:1

以下代码应该打印临时表#table_A中包含的所有行:

create table #table_A
    (
     ID                  int          NULL ,
     foo                 int          NULL 
    )

go

insert into #table_A values (1, 2)
insert into #table_A values (2, 3)
go

declare c_table_A cursor                     
    for select *                                    
          from #table_A                      
         order                                      
            by 1                                                                      

open c_table_A                               
  fetch c_table_A                            
  while @@sqlstatus = 0                             
    begin                                           
      print '%1!', c_table_A                 
      fetch c_table_A                        
    end                                             
close c_table_A   


go  

但是,它导致以下错误消息:

DECLARE CURSOR must be the only statement in a query batch.  

如何打印(临时)表中包含的所有行?


这里是我提出问题的另一种方式:

我正在尝试做类似的事情:

open c_table_A                               
  fetch c_table_A into @record_variable                           
  while @@sqlstatus = 0                             
    begin                                           
      print '%1!', @record_variable
      fetch c_table_A into @record_variable                       
    end                                             
close c_table_A   

是否可以在sybase中声明包含表的整个行的变量?


P.S .:仅使用“从...中选择*”对我不起作用。在打印行之前,我需要对每行进行一些处理。 (我的问题应该集中在基本部分上,这就是为什么我没有详细介绍每行需要做的其他事情的更多细节)

sql sybase-ase cursors
1个回答
0
投票

感谢您的澄清。

在SQL批处理中,而不是在存储过程中,游标声明必须与使用它的批处理分开,因此go与后续批处理之间必须有一个declare cursor

抱歉,无法在Sybase ASE中定义“行变量”。返回到变量的每一列必须为其声明一个变量。在下面的示例中,@ id和@foo被声明为与表中的id和foo列相同的类型。其他RDBMS确实具有“记录数据类型”,但不幸的是没有Sybase ASE。

在使用游标之前,在大型表上游标相对较慢,您可能可以在select语句中执行其他处理。如果存在条件逻辑case ... when ... then ... else ... end可能很有用,尽管无法从select语句内直接调用存储过程,则可以调用SQL用户定义的函数。如果您需要帮助,那可能是一个单独的问题。

我也添加了deallocate cursor语句,它是语法的一部分,并释放了与您的连接关联的内部工作空间。

您可能需要在运行批处理之前执行set nocount on,它会删除有时令人讨厌的(1 row affected)消息。


set nocount on
go

create table #table_A
    (
     ID                  int          NULL ,
     foo                 int          NULL 
    )
go

insert into #table_A values (1, 2)
insert into #table_A values (2, 3)
go

declare c_table_A cursor                     
    for select *                                    
          from #table_A                      
         order                                      
            by 1                                                                      
go

declare
    @id     int,
    @foo    int

open c_table_A                               
fetch c_table_A into @id, @foo

while @@sqlstatus = 0                             
begin                                           
    print 'id: %1! foo: %2!', @id, @foo
    fetch c_table_A into @id, @foo
end                                             

close c_table_A   
go 

deallocate cursor c_table_A
go
© www.soinside.com 2019 - 2024. All rights reserved.