SQLite有游标吗?

时间:2009-12-09 21:22:43

标签: sqlite cursor

我想知道我是否可以在SQLite中运行以下过程:

set nocount on

select T.ID, max(T.SerialNo) as SerialNo
into #Tmp_Ticket_ID
from Ticket as T, Ticket as inserted
where t.ID = inserted.ID
group by T.id having count(*) > 1

declare zeiger cursor for
    select SerialNo
    from #Tmp_Ticket_ID

declare @SerialNo int

OPEN Zeiger  
FETCH NEXT FROM zeiger INTO @SerialNo 
WHILE (@@fetch_status <> -1)  
BEGIN  
    IF (@@fetch_status <> -2)  
    BEGIN  
        update T
            set ID = (select max(id) + 1 from Ticket)
        from ticket AS T, #Tmp_Ticket_ID as I
        where t.serialNo = i.serialno
        and I.Serialno = @SerialNo
    END
    FETCH NEXT FROM zeiger INTO @SerialNo
END  
CLOSE Zeiger  
DEALLOCATE Zeiger  
DROP TABLE #Tmp_Ticket_ID

这是ms-sql2000中的一个小程序,它清除了以下结构的给定表Ticket中的Ticket_id的两倍:

create table Ticket (serialNo int identity(1,1) not null
    , ID as int not null
    , Ticket_issue as varchar(50)
    , some_more_field varchar(500))

由于来自不同数据库的简单合并,ticket_id变得不唯一。为了通过重新编号来修复我开发了这个过程,但现在我们在SQLite-db上遇到了类似的问题。

1 个答案:

答案 0 :(得分:4)

delete from Ticket
where exists
 (select rowid from Ticket t2
   where t2.ID = Ticket.ID and t2.rowid < Ticket.rowid)

rowid是始终存在的SQLite btree索引列。

感谢Martin Engelschalk在2009-08-17的SQLite邮件列表上。