简体   繁体   English

对于PL / SQL中的循环和列

[英]For loop and columns in PL/SQL

I am new in PL/SQL. 我是PL / SQL的新手。 I have a problem with loop in this language. 我在这种语言中遇到循环问题。 I' d like to make loop like this: 我想像这样循环:

FOR nr IN 1..102 
LOOP
  DBMS_OUTPUT.PUT_LINE(nr);
  IF rec.column_||nr IS NULL
    THEN
    DBMS_OUTPUT.PUT_LINE('test');
  END IF;
END LOOP;

I have created a cursor. 我创建了一个游标。 As you can see I' d like to check all column with names column from column_1 to column_102. 如您所见,我想检查列号为column_1到column_102的所有列。 Unfortunately || 不幸的是|| operator does not work for this situation. 运营商不适用于这种情况。 Do you know some solution to my problem? 你知道我的问题的一些解决方案吗?

You can do this with dynamic PL/SQL . 您可以使用动态PL / SQL执行此操作。 Use an EXECUTE IMMEDIATE statement to execute a string argument as PL/SQL, which you can make up with || 使用EXECUTE IMMEDIATE语句将字符串参数作为PL / SQL执行,您可以使用|| as it was intended in the question. 因为它是在问题中的意图。

Example: 例:

BEGIN 
    FOR nr IN 1..102 
    LOOP
        DBMS_OUTPUT.PUT_LINE(nr);
        EXECUTE IMMEDIATE 
            'BEGIN ' || 
            'IF rec.column.' || nr ||' is null THEN ' ||
                'DBMS_OUTPUT.PUT_LINE(''test''); ' ||
            'END IF; ' || 
            'END; ';
    END LOOP;
END;

Or you could also assign rec.column.' || nr ||' is null 或者您也可以指定rec.column.' || nr ||' is null rec.column.' || nr ||' is null rec.column.' || nr ||' is null to a variable and make the PUT_LINE outside the EXECUTE IMMEDIATE part: 变量rec.column.' || nr ||' is null并使PUT_LINE超出EXECUTE IMMEDIATE部分:

UPDATE : It seems it is not possible to bind BOOLEAN variables, so I've modified the example to use a NUMBER . 更新 :似乎无法绑定BOOLEAN变量,所以我修改了示例以使用NUMBER

UPDATE 2: There is a possible efficiency improvement, altough maybe not suitable in this case. 更新2:可能的效率提高,尽管可能不适合这种情况。 Use a constant VARCHAR for the dynamic SQL, and pass in nr with a binded variable. 对动态SQL使用常量VARCHAR ,并使用绑定变量传入nr This is even more efficient than using native SQL if in a large loop. 如果在大型循环中,这比使用本机SQL更有效。 I don't think 'rec.column.:arg is null would execute as 'rec.column.1 is null , though. 我不认为'rec.column.:arg is null将执行,因为'rec.column.1 is null

 DECLARE
    isnull NUMBER;
 BEGIN 
    FOR nr IN 1..102 
    LOOP
        DBMS_OUTPUT.PUT_LINE(nr);
        EXECUTE IMMEDIATE 
            'BEGIN ' || 
                'IF rec.column.' || nr ||' IS NULL THEN ' || 
                    ':x:=1; ' || 
                'ELSE ' ||
                    ':x:=0; ' ||
                'END IF; ' ||
            'END; ' 
            USING OUT isnull;
        IF isnull = 1 THEN 
            DBMS_OUTPUT.PUT_LINE('test');
        END IF;
    END LOOP;
END;

UPDATE 3 : Seeing that: 更新3 :看到:

  • It is not possible to access rec inside the dynamic SQL statement because it is undefined (out of scope), 无法在动态SQL语句中访问rec ,因为它未定义(超出范围),

  • It seems not possible to pass a non-sql type as an argument to the dynamic statement (record, cursor) 似乎不可能将非sql类型作为参数传递给动态语句(记录,游标)

A possible workaround is to bind some id columns (SQL Type) to the dynamic statement, and use a select clause to find out if the current column is null: 一种可能的解决方法是将一些id列(SQL Type)绑定到dynamic语句,并使用select子句来查找当前列是否为null:

DECLARE
        isnull NUMBER;
        rec_id NUMBER; -- Identifier of the fetched record
     BEGIN 
        rec_id := rec.id;
        FOR nr IN 1..102 
        LOOP
            DBMS_OUTPUT.PUT_LINE(nr);
            EXECUTE IMMEDIATE 
                'SELECT 1 FROM my_table WHERE id = :idarg ' ||
                   ' AND column_' || nr || ' IS NULL'
              INTO isnull USING rec_id;
            IF isnull = 1 THEN 
                DBMS_OUTPUT.PUT_LINE('test');
            END IF;
        END LOOP;
    END;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM