0

在 Oracle 中,我们这样做:

def TNAME=&1
create table &TNAME (foo varchar(10));

如何为 DB2 做同样的事情?约束是:

1. The create table statement is generated when TNAME is unknown
2. The create table statement is in a file which cannot be modified
3. Cannot create a temporary file with TNAME substituted by awk
4. Essentially we want to pass in the table name at run time as an argument

可能的?

4

2 回答 2

1

EXECUTE IMMEDIATE语句几乎可以用在任何由 BEGIN 和 END 包围的 SQL 块中,例如存储过程或匿名复合语句。

CREATE PROCEDURE create_table_from_file( IN fileDir VARCHAR( 1024 ), 
                                         IN fileName VARCHAR( 128 ),
                                         IN tabSchema VARCHAR( 128 ),
                                         IN tabName VARCHAR( 128 )
                                        )
BEGIN
DECLARE fh UTL_FILE.FILE_TYPE;
DECLARE sqlStmt VARCHAR( 32672 );
DECLARE currentLine VARCHAR( 32672 );
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE SQLSTATE CHAR(5) DEFAULT '00000';
DECLARE SQLSTATE1 CHAR(5) DEFAULT '00000';
DECLARE CONTINUE HANDLER FOR SQLSTATE 'ORANF' SET SQLSTATE1 = SQLSTATE;

SET sqlStmt = 'CREATE TABLE ' 
    || CASE WHEN ( NULLIF( tabSchema, '' ) IS NOT NULL ) 
       THEN RTRIM( tabSchema ) || '.'
       ELSE '' END || tabName;
CALL UTL_DIR.CREATE_OR_REPLACE_DIRECTORY( 'createTablePath', fileDir );
SET fh = UTL_FILE.FOPEN( 'createTablePath', fileName, 'r' );
loop1: LOOP
    CALL UTL_FILE.GET_LINE( fh, currentLine );
    IF SQLSTATE1 = 'ORANF' THEN -- NO DATA FOUND
        LEAVE loop1;
    END IF;
    SET sqlStmt = sqlStmt || ' ' || currentLine ;
END LOOP;
EXECUTE IMMEDIATE sqlStmt;
END
@
于 2012-01-08T06:24:53.547 回答
0

我创建了一个文件abc,其中包含:

create table TNAME (foo varchar(10))

然后我在 Unix 命令行上做了:

sed s/TNAME/xyz/g abc | db2

xyz它根据我的需要创建了一个在 DB2 中调用的表。谢谢。

于 2012-01-02T00:00:27.193 回答