在存储过程中使用游标

时间:2014-04-17 17:00:36

标签: sql-server tsql

DECLARE MAP_CURSOR CURSOR FOR
    select C.OBJECT,C.CLAIM,RELID
            from EPM_ E WITH (NOLOCK) 
            join #ABCD C WITH (NOLOCK)
            on E.RELID = C.OBJECT

OPEN MAP_CURSOR
FETCH NEXT FROM MAP_CURSOR INTO @OBJECT,@CLAIM
WHILE @@FETCH_STATUS = 0
    BEGIN
        BREAK
    END
    IF @@FETCH_STATUS <> 0
        BEGIN
          SET @PAYMENT_ID = (SELECT PAYMENT_ID FROM PAYMENT 
                                   WHERE CLAIM = @OBJECT)
        END

FETCH NEXT FROM MAP_CURSOR INTO @OBJECT,@CLAIM
CLOSE MAP_CURSOR
DEALLOCATE MAP_CURSOR

我试图找出表中是否存在所有ID与epm表匹配,然后需要结束其他需要从付款表中选择paymentid来处理它。

任何人都可以帮我解决吗?

1 个答案:

答案 0 :(得分:0)

根据您的评论,这将获得没有与#ABCD相关的epm记录的所有付款。它还会显示临时表中数据没有付款的位置。

SELECT c.*,p.*
FROM #ABCD C
left join EPM_ E on E.RELID = C.OBJECT
left join PAYMENT p on p.CLAIM=C.OBJECT
where E.RELID is null