gpt4 book ai didi

sql - 在 SYBASE 中替代游标?

转载 作者:行者123 更新时间:2023-12-02 06:40:07 24 4
gpt4 key购买 nike

假设我要处理 10 张借书证,每张卡都有客户值(例如成员(member)编号、成员(member)姓名......),我需要为每张卡更新一个值。

如果我想从数据库中抓取所有十行,但一次只想更新每一行,是否有游标的替代方法?我知道 while 循环可能有效,但我如何才能在每次循环时都抓取一行,直到我完成所有 10 张卡片?

最佳答案

不需要使用游标。我大部分时间都使用这个:

declare @uid int -- this is the type unique index on the table you're updating

-- Copy out the unique ids of the rows you want to update to a temporary table
select uid into #temp from customers -- you can use a where condition here

-- Loop through the rows of the temp table
while exists (select 1 from #temp)
begin
set rowcount 1
select @uid = uid from #temp -- pull one uid from the temp table
set rowcount 0
delete from #temp where uid = @uid -- delete that uid from the temp table

-- Do something with the uid you have
update customers set name = 'Joe Shmoe' where uid = @uid

end

关于sql - 在 SYBASE 中替代游标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9539918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com