3

从 SQL Server 切换到 Oracle。如何将字符串连接到循环内的变量?在 SQL Server 中,我会写类似

DECLARE @var varchar(max);
WHILE ([I'm doing something or whatever on a table])
BEGIN
    SET @var = @var + ' additional text';
END
SELECT @var AS 'result';

到目前为止,这就是我在 Oracle 中的存储过程中所拥有的。(将其演变为 NHibernate 的 ClassMap 文件的自动生成器)

CREATE OR REPLACE PROCEDURE WWNA.UT_MAPPINGHELPER
(
    TABLE_NAME IN ALL_TABLES.TABLE_NAME%TYPE
    , OWNER_NAME IN ALL_TABLES.OWNER%TYPE
    , CLASS_DEFINITION IN OUT VARCHAR2
)
   IS
   CURSOR TABLE_INFO_CUR
   IS
    SELECT 
        T.TABLE_NAME
        , C.COLUMN_NAME
        ,C.DATA_TYPE
        ,C.DATA_LENGTH
        ,C.DATA_PRECISION
        ,C.NULLABLE
    FROM 
        ALL_TABLES T
        JOIN
        ALL_TAB_COLUMNS C
            ON T.TABLE_NAME = C.TABLE_NAME
                AND T.OWNER = C.OWNER
    WHERE
        T.TABLE_NAME = :TABLE_NAME
        AND T.OWNER = :OWNER_NAME;
    TABLE_INFO_ROW TABLE_INFO_CUR%ROWTYPE;
    build_sql varchar2;
BEGIN
    --VARIABLE build_sql varchar2(4000);
    :build_sql := '';
    OPEN TABLE_INFO_CUR;

    LOOP
        FETCH TABLE_INFO_CUR INTO TABLE_INFO_ROW;
        EXIT WHEN TABLE_INFO_CUR%NOTFOUND;
        SELECT (:build_sql || ' ' || TABLE_INFO_ROW.TABLE_NAME || ' ' || TABLE_INFO_ROW.COLUMN_NAME || ' ' || TABLE_INFO_ROW.DATA_TYPE) INTO :build_sql;
    END LOOP;
    :CLASS_DEFINITION := :build_sql;  

    --DBMS_OUTPUT.put_line (CLASS_DEFINITION);


END;

谢谢

4

2 回答 2

2
CREATE OR REPLACE PROCEDURE WWNA.UT_MAPPINGHELPER
(
    TABLE_NAME IN ALL_TABLES.TABLE_NAME%TYPE
    , OWNER_NAME IN ALL_TABLES.OWNER%TYPE
    , CLASS_DEFINITION IN OUT VARCHAR2
)
   IS
   CURSOR TABLE_INFO_CUR
   IS
    SELECT 
        T.TABLE_NAME
        , C.COLUMN_NAME
        ,C.DATA_TYPE
        ,C.DATA_LENGTH
        ,C.DATA_PRECISION
        ,C.NULLABLE
    FROM 
        ALL_TABLES T
        JOIN
        ALL_TAB_COLUMNS C
            ON T.TABLE_NAME = C.TABLE_NAME
                AND T.OWNER = C.OWNER
    WHERE
        T.TABLE_NAME = :TABLE_NAME
        AND T.OWNER = :OWNER_NAME;

    build_sql varchar2(32767);
BEGIN
    --build_sql := ''; --Not needed, build_sql is already initialised to NULL


    FOR cur_rec IN TABLE_INFO_CUR
    LOOP
        build_sql := build_sql || ' ' || 
                     cur_rec.TABLE_NAME || ' ' || 
                     cur_rec.COLUMN_NAME || ' ' || 
                     cur_rec.DATA_TYPE;
    END LOOP;
    CLASS_DEFINITION := build_sql;  

    --DBMS_OUTPUT.put_line (CLASS_DEFINITION);


END;

您需要定义变量CLASS_DEFINITION

使用CURSOR FOR LOOP可以为您打开和关闭游标以及批量获取结果。

您也可以使用CONCAT函数而不是|| 功能。

于 2013-06-26T19:07:11.520 回答
1

如果要对过程中的build_sql值执行此操作,请省略前导冒号。这是一个参数化的值,这里不适用。

还要build_sql在顶部声明变量的长度。这有点令人困惑,因为您可以省略参数的长度,但您需要为声明的变量设置长度。

这是您的 proc 的截断版本,显示了build_sql操作。另请注意,CLASS_DEFINITION参数变量也不应该有冒号前缀:

CREATE OR REPLACE PROCEDURE WWNA.UT_MAPPINGHELPER
(
    TABLE_NAME IN ALL_TABLES.TABLE_NAME%TYPE
    , OWNER_NAME IN ALL_TABLES.OWNER%TYPE
    , CLASS_DEFINITION IN OUT VARCHAR2
)
IS
   ...
   build_sql varchar2(4000);
BEGIN
   build_sql := '';
   ...
   LOOP
      build_sql := build_sql || 'whatever';
   END LOOP;
   CLASS_DEFINITION := build_sql;  
END;

另请注意,您不需要该build_sql := ''行 - 变量值将默认为空白。或者您可以在声明时分配一个初始值:

CREATE OR REPLACE PROCEDURE WWNA.UT_MAPPINGHELPER
(
    TABLE_NAME IN ALL_TABLES.TABLE_NAME%TYPE
    , OWNER_NAME IN ALL_TABLES.OWNER%TYPE
    , CLASS_DEFINITION IN OUT VARCHAR2
)
IS
   ...
   build_sql varchar2(4000) := 'start of my sql';
BEGIN
   ...
于 2013-06-26T19:06:50.323 回答