gpt4 book ai didi

sql-server-2008 - SQL从游标中获取记录

转载 作者:行者123 更新时间:2023-12-04 14:14:58 25 4
gpt4 key购买 nike

我有记录列表,并创建了游标来循环每条记录并检查某些条件,如果满足我的游标,则返回记录如下:

DECLARE @ID int
DECLARE @FromDate datetime, @ToDate datetime
DEClare @expid as int
set @expid = 839
DECLARE IDs CURSOR FOR
select patpid,fromdate,todate from tdp_ProviderAccomodationTariffPlan where fk_patid = 162 and fk_pacid = 36

OPEN IDs
FETCH NEXT FROM IDs into @ID,@FromDate,@ToDate
WHILE @@FETCH_STATUS = 0
BEGIN
print @ID
print @FromDate
print @ToDate

--SELECT patpid,rate,SType FROM tdp_ProviderAccomodationTariffPlan
--WHERE ('2012-12-27' BETWEEN @FromDate AND @ToDate) and fk_patid = 162 and fk_pacid = 36

FETCH NEXT FROM IDs into @ID,@FromDate,@ToDate

END
CLOSE IDs
DEALLOCATE IDs

in loop cursor fetch record of its id is '839' ,请帮我解决问题。

最佳答案

用 WHILE 循环替换光标以获得更快的性能,如下所示:

select identity(int,1,1) as id, patpid,fromdate,todate
INTO #temp1
from tdp_ProviderAccomodationTariffPlan
where fk_patid = 162 and fk_pacid = 36

declare @index int
declare @count int

select @count = count(*) from @temp1
set @index = 1

declare @patpid int
declare @fromdate datetime
declare @todate datetime

while @index <= @count
begin

select @patid = patid,
@fromdate = fromdate,
@todate = todate
from #temp1
where id = @index

-- do your logic here

set @index= @index + 1
end

drop table #temp1

关于sql-server-2008 - SQL从游标中获取记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15195364/

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