在SQL SERVER中实现for循环登录

问题描述 投票:1回答:1
FOR deposit_indx IN 1..deposit_table_obj.COUNT
    LOOP     
      FOR i IN 1..category_table_obj.COUNT LOOP
        IF(deposit_table_obj(deposit_indx).borrower_category_code IS NULL
          OR deposit_table_obj(deposit_indx).sector_code IS NULL
          OR deposit_table_obj(deposit_indx).sub_sector_code IS NULL) 
        THEN
          excludeAcct := 1;                                
          EXIT;
        ELSIF(INSTR(category_table_obj(i).borrowercat_code, deposit_table_obj(deposit_indx).borrower_category_code) > 0
          AND INSTR(category_table_obj(i).sector_code, deposit_table_obj(deposit_indx).sector_code) > 0
          AND INSTR(category_table_obj(i).sub_sector_code, deposit_table_obj(deposit_indx).sub_sector_code) > 0) 
        THEN
          excludeAcct := 0;
          EXIT;        
        ELSE
          excludeAcct := 1;                                
        END IF;
      END LOOP;

      IF(excludeAcct = 1) THEN
        exclusion_table_obj.EXTEND;
        exclusion_table_obj(ex_indx).acid        :=  deposit_table_obj(deposit_indx).acid;
        exclusion_table_obj(ex_indx).balance     :=  deposit_table_obj(deposit_indx).balance;
        exclusion_table_obj(ex_indx).rep_date    :=  deposit_table_obj(deposit_indx).rep_date;
        ex_indx := ex_indx + 1;     
        excludeAcct := 0;                                                  
      END IF;

    END LOOP;

我正在尝试将此部分查询实现到SQL SERVER中。我打算从deposite_table中选择count()并从category_table中选择count()以在while循环条件中使用,还是应该使用游标?此外,这些表将是临时表deposit_table和category_table,并从存储过程调用。

sql-server oracle
1个回答
0
投票

正如其他人所说,我认为你不需要游标或循环。如果你知道怎么问它,SQL就会为你做那些事情。

据我所知,你的代码在某些条件下将行插入exclusion_table_obj,所以我认为它会变成这样的INSERT语句:

INSERT INTO exclusion_table_obj(acid, balance, rep_date)
SELECT d.acid, d.balance, d.rep_date
FROM deposit_table_obj d
WHERE d.borrower_category_code IS NULL OR d.sector_code IS NULL OR d.sub_sector_code IS NULL
OR NOT EXISTS (SELECT *
               FROM category_table_obj c
               WHERE c.borrowercat_code = d.borrower_category_code
               AND c.sector_code = d.sector_code
               AND c.sub_sector_code = d.sub_sector_code)
© www.soinside.com 2019 - 2024. All rights reserved.