生成动态SQL_insert update select 语句

Posted xiaogaokui

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了生成动态SQL_insert update select 语句相关的知识,希望对你有一定的参考价值。

快速生成insert update select 语句

declare

sText VARCHAR2(3000);

sTable varchar2(30);

begin

sTable := ‘&Tablename‘;

select get_sql_insert(sTable) INTO sText from dual;

DBMS_OUTPUT.put_line(sText);

DBMS_OUTPUT.put_line(‘‘);

DBMS_OUTPUT.put_line(‘‘);

select get_sql_Update(sTable) INTO sText from dual;

DBMS_OUTPUT.put_line(sText);

DBMS_OUTPUT.put_line(‘‘);

DBMS_OUTPUT.put_line(‘‘);

select get_sql_select(sTable) INTO sText from dual;

DBMS_OUTPUT.put_line(sText);

end;

/

 

CREATE OR REPLACE FUNCTION GET_SQL_INSERT(STABLENAME IN VARCHAR2) RETURN VARCHAR2 IS

/*

-- 用途 : 获取表全部字段的插入(INSERT)语句

*/

RESULT VARCHAR2(3000);

I INTEGER;

BEGIN

I:= 0;

RESULT := ‘INSERT INTO ‘|| UPPER(STABLENAME) ||‘(‘ ;

FOR CUR IN (SELECT COLUMN_NAME FROM USER_TAB_COLS WHERE UPPER(TABLE_NAME) = UPPER(STABLENAME) ORDER BY COLUMN_ID ) LOOP

IF I = 0 THEN

RESULT := RESULT || CUR.COLUMN_NAME;

ELSE

RESULT := RESULT ||‘,‘ ||CUR.COLUMN_NAME;

END IF;

I:= I+1;

END LOOP;

RESULT := RESULT || ‘ ) VALUES ( ‘;

I:= 0;

FOR CUR IN (SELECT COLUMN_NAME FROM USER_TAB_COLS WHERE UPPER(TABLE_NAME) = UPPER(STABLENAME) ORDER BY COLUMN_ID ) LOOP

IF I = 0 THEN

RESULT := RESULT ||‘:‘||CUR.COLUMN_NAME;

ELSE

RESULT := RESULT ||‘,:‘ ||CUR.COLUMN_NAME;

END IF;

I:= I+1;

END LOOP;

RESULT := RESULT || ‘ ) ‘;

RETURN(RESULT);

END GET_SQL_INSERT;

/

 

CREATE OR REPLACE FUNCTION GET_SQL_UPDATE(STABLENAME IN VARCHAR2)

RETURN VARCHAR2 IS

/*

-- 用途 : 获取表全部字段的更新(UPDATE)语句

*/

RESULT VARCHAR2(3000);

PK_COL VARCHAR2(30);

I INTEGER;

BEGIN

I := 0;

RESULT := ‘UPDATE ‘ || UPPER(STABLENAME) || ‘ SET ‘;

FOR CUR IN (SELECT COLUMN_NAME

FROM USER_TAB_COLS

WHERE UPPER(TABLE_NAME) = UPPER(STABLENAME)

ORDER BY COLUMN_ID) LOOP

IF I = 0 THEN

RESULT := RESULT || CUR.COLUMN_NAME || ‘= :‘ || CUR.COLUMN_NAME;

ELSE

RESULT := RESULT || ‘,‘ || CUR.COLUMN_NAME || ‘= :‘ ||

CUR.COLUMN_NAME;

END IF;

I := I + 1;

END LOOP;

RESULT := RESULT || ‘ WHERE ‘;

BEGIN

SELECT UL.COLUMN_NAME

INTO PK_COL

FROM USER_CONSTRAINTS UC, USER_CONS_COLUMNS UL

WHERE UC.CONSTRAINT_NAME = UL.CONSTRAINT_NAME

AND UC.TABLE_NAME = UPPER(STABLENAME)

AND CONSTRAINT_TYPE = ‘P‘;

EXCEPTION

WHEN OTHERS THEN

PK_COL := ‘‘;

END;

IF PK_COL IS NOT NULL THEN

RESULT := RESULT || PK_COL || ‘1 = :‘ || PK_COL || ‘1‘;

END IF;

RETURN(RESULT);

END GET_SQL_UPDATE;

 

 

/

 

CREATE OR REPLACE FUNCTION GET_SQL_SELECT(STABLENAME VARCHAR2,

OtherName VARCHAR2 default ‘‘) RETURN VARCHAR2 IS

/*

-- 用途 : 获取表全部字段的查询(SELECT)语句

*/

RESULT VARCHAR2(3000);

I INTEGER;

PreOtherName VARCHAR2(31);

BEGIN

I:= 0;

IF nvl(TRIM(otherName),‘ ‘) = ‘ ‘ THEN

PreOtherName := ‘‘;

ELSE

PreOtherName := TRIM(otherName) ||‘.‘;

END IF;

 

 

RESULT := ‘SELECT ‘;

FOR CUR IN (SELECT COLUMN_NAME FROM USER_TAB_COLS WHERE UPPER(TABLE_NAME) = UPPER(STABLENAME) ORDER BY COLUMN_ID ) LOOP

IF I = 0 THEN

RESULT := RESULT || PreOtherName||CUR.COLUMN_NAME;

ELSE

RESULT := RESULT ||‘,‘ ||PreOtherName||CUR.COLUMN_NAME;

END IF;

I:= I+1;

END LOOP;

RESULT := RESULT || ‘ FROM ‘|| UPPER(STABLENAME)||‘ ‘||TRIM(otherName);

RETURN(RESULT);

END GET_SQL_SELECT;

 

以上是关于生成动态SQL_insert update select 语句的主要内容,如果未能解决你的问题,请参考以下文章

如何根据 HTML FORM 中定义的变量动态生成 MYSQL UPDATE 语句

如何利用AngularJS框架遍历生成动态的下拉框

如何用SQL代码将动态查询结果赋值给变量?

Hibernate,JPA注解@DynamicInsert和@DynamicUpdate

SAP 动态选择屏幕实例

SAP 动态选择屏幕实例