温馨提示:本文翻译自stackoverflow.com,查看原文请点击:sql - Sybase ASE: how to print all table rows using cursor?
sql sybase-ase cursors

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

发布于 2020-03-30 21:51:28

以下代码应该打印临时表#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中声明包含表的整个行的变量?


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

查看更多

提问者
Thomas_SO
被浏览
12
Richard Crossley 2020-02-04 23:22

感谢您的澄清。

在SQL批处理中,游标声明必须与使用该批处理的批处理分开,而不是在存储过程中使用,因此godeclare 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