從SQL Server切換到Oracle。如何將字符串連接到循環內的變量?在SQL Server中,我會寫類似於Oracle相當於T-SQL SET @var = @var +'';
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存儲過程中使用的存儲過程。如果你想在過程中的build_sql
值要做到這一點,離開了領導冒號(將其演變爲類映射文件的AUTOGEN NHibernate的)
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;
感謝
什麼是w /變量之前的所有冒號?不要以爲你需要那樣。無論如何,你已經使用了concat操作符||,所以對於變量x,將是「x:= x ||;」 –
OldProgrammer