简体   繁体   English

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

[英]Sybase ASE: how to print all table rows using cursor?

The following code is supposed to print all rows contained in the temporary table #table_A:以下代码应该打印临时表#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  

However, it results in the following error message:但是,它会导致以下错误消息:

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

How can I print all rows contained in a (temporary) table?如何打印(临时)表中包含的所有行?


Here's another way of putting my question:这是提出我的问题的另一种方式:

I'm trying to do something like that:我正在尝试做这样的事情:

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   

Is there a way to declare a variable containing a whole row of a table in sybase?有没有办法在sybase中声明一个包含整行表的变量?


PS: just using "select * from ..." doesn't work for me. PS:仅使用“select * from ...”对我不起作用。 I need to do some stuff with each of the rows before printing the row.在打印行之前,我需要对每一行做一些事情。 (My question is supposed to focus on the essential part, which is why I didn't go into any further details regarding other things I need to do with each row) (我的问题应该集中在基本部分,这就是为什么我没有详细说明我需要对每一行做的其他事情)

Thanks for the clarification.感谢您的澄清。

In a SQL batch, rather than a stored procedure the cursor declaration must be separate from the batch that uses it, hence there needs to be a go between the declare cursor and the subsequent batch.在SQL批处理,而不是存储过程中的游标声明必须是使用它的批次分开的,因此需要有一个go之间declare cursor和下一批次。

There is no way to define a "row variable" in Sybase ASE, sorry.抱歉,无法在 Sybase ASE 中定义“行变量”。 Each column returned into a variable must have a variable declared for it.返回到变量中的每一列都必须为其声明一个变量。 In the example below @id and @foo are declared as the same types as the columns id and foo in the table.在下面的示例中,@id 和 @foo 被声明为与表中的列 id 和 foo 相同的类型。 Other RDBMS do have "record data types", but unfortunately not Sybase ASE.其他 RDBMS 确实有“记录数据类型”,但遗憾的是没有 Sybase ASE。

Before committing yourself to using a cursor, which on a large table would be relatively slow, you might be able to perform your other processing in a select statement.在承诺使用游标之前,游标在大表上会相对较慢,您可以在 select 语句中执行其他处理。 If there is conditional logic case ... when ... then ... else ... end could prove useful, though calling a stored procedure directly from within a select statement is not possible, you can call a SQL user defined function.如果存在条件逻辑case ... when ... then ... else ... end可能证明是有用的,尽管直接从 select 语句中调用存储过程是不可能的,但您可以调用 SQL 用户定义函数。 That's probably a separate question if you need help.如果您需要帮助,这可能是一个单独的问题。

I've added a deallocate cursor statement as well, it's part of the syntax and frees up internal workspaces associated with your connection.我还添加了一个deallocate cursor语句,它是语法的一部分并释放与您的连接相关联的内部工作区。

You may want to execute set nocount on before running the batch, it removes the sometimes annoying (1 row affected) message.您可能希望在运行批处理之前执行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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM