sqlparser/statements.cpp
// DECLARE GLOBAL TEMPORARY TABLE in DB2
DECLARE GLOBAL TEMPORARY TABLE in DB2
DECLARE GLOBAL TEMPORARY TABLE in DB2
bool SqlParser::ParseDeclareGlobalTemporaryTable(Token *declare, Token *global)
{
	if(declare == NULL || global == NULL)
		return false;
	if(global->Compare("GLOBAL", L"GLOBAL", 6) == false)
		return false;
	Token *temporary = GetNextWordToken("TEMPORARY", L"TEMPORARY", 9);
	if(temporary == NULL)
		return false;
	Token *table = GetNextWordToken("TABLE", L"TABLE", 5);
	if(table == NULL)
		return false;
    STATS_DECL
	PL_STMS_STATS_V("DECLARE GLOBAL TEMPORARY TABLE", declare);
	Token *name = GetNextIdentToken();
	if(name == NULL)
		return false;
	if(_source == SQL_DB2 && _target != SQL_DB2)
	{
		if(name->Compare("SESSION.", L"SESSION.", 0, 8) == true)
		{
			TokenStr name2;
			name2.Append(name, 8, name->len - 8);
			Token::ChangeNoFormat(name, name2);
		}
		else
		if(_target == SQL_SQL_SERVER)
		{
			TokenStr name2;
			name2.Append("#", L"#", 1);
			name2.Append(name);
			Token::ChangeNoFormat(name, name2);
		}
	}
	_spl_created_session_tables.Add(name);
	Token *as = GetNextWordToken("AS", L"AS", 2);
	 (void) GetNextCharToken('(', L'(');
	Token *select = GetNextSelectStartKeyword();
	Token *from_end = NULL;
	Token *where_end = NULL;
	if(as == NULL)
	{
		ListW pkcols;
		ParseCreateTableColumns(declare, name, pkcols, NULL, NULL, NULL, NULL, NULL, NULL);
	}
	else
		ParseSelectStatement(select, 0, SQL_SEL_CREATE_TEMP_TABLE_AS, NULL, NULL, NULL, NULL, NULL, NULL, &from_end, &where_end);
	 (void) GetNextCharToken(')', L')');
	bool no_data;
	Token *options_start = NULL;
	Token *options_end = NULL; 
	ParseTempTableOptions(name, &options_start, &options_end, &no_data);
	if(_target == SQL_ORACLE)
	{
		Token::Change(declare, "CREATE", L"CREATE", 6);		
		if(_spl_scope != 0)
		{
			Token *last = Nvl(GetNextCharToken(';', L';'), GetLastToken());
			PrependCopy(_spl_start, declare, last, false);
			Prepend(_spl_start, "\n\n", L"\n\n", 2);
			Token::Remove(declare, last);
		}
		if(no_data == true)
		{
			if(where_end == NULL)
				Append(from_end, " WHERE 1=0", L" WHERE 1=0", 10, declare);
			else
				Append(where_end, " AND 1=0", L" AND 1=0", 8, declare);
		}
		if(select != NULL && options_start != NULL)
		{
			PrependSpaceCopy(as, options_start, options_end, false);
			Token::Remove(options_start, options_end);
		}
	}
	else
	if(_target == SQL_SQL_SERVER)
	{
		Token::Change(declare, "CREATE", L"CREATE", 6);		
		Token::Remove(global, temporary);
	}
	return true;
}
bool SqlParser :: ParseDeclareGlobalTemporaryTable ( Token * declare , Token * global ) { if ( declare == NULL || global == NULL ) return false ; if ( global -> Compare ( " " , L" " , 6 ) == false ) return false ; Token * temporary = GetNextWordToken ( " " , L" " , 9 ) ; if ( temporary == NULL ) return false ; Token * table = GetNextWordToken ( " " , L" " , 5 ) ; if ( table == NULL ) return false ; STATS_DECL PL_STMS_STATS_V ( " " , declare ) ; Token * name = GetNextIdentToken ( ) ; if ( name == NULL ) return false ; if ( _source == SQL_DB2 && _target != SQL_DB2 ) { if ( name -> Compare ( " " , L" " , 0 , 8 ) == true ) { TokenStr name2 ; name2 . Append ( name , 8 , name -> len - 8 ) ; Token :: ChangeNoFormat ( name , name2 ) ; } else if ( _target == SQL_SQL_SERVER ) { TokenStr name2 ; name2 . Append ( " " , L" " , 1 ) ; name2 . Append ( name ) ; Token :: ChangeNoFormat ( name , name2 ) ; } } _spl_created_session_tables . Add ( name ) ; Token * as = GetNextWordToken ( " " , L" " , 2 ) ; ( void ) GetNextCharToken ( ' ' , L' ' ) ; Token * select = GetNextSelectStartKeyword ( ) ; Token * from_end = NULL ; Token * where_end = NULL ; if ( as == NULL ) { ListW pkcols ; ParseCreateTableColumns ( declare , name , pkcols , NULL , NULL , NULL , NULL , NULL , NULL ) ; } else ParseSelectStatement ( select , 0 , SQL_SEL_CREATE_TEMP_TABLE_AS , NULL , NULL , NULL , NULL , NULL , NULL , & from_end , & where_end ) ; ( void ) GetNextCharToken ( ' ' , L' ' ) ; bool no_data ; Token * options_start = NULL ; Token * options_end = NULL ; ParseTempTableOptions ( name , & options_start , & options_end , & no_data ) ; if ( _target == SQL_ORACLE ) { Token :: Change ( declare , " " , L" " , 6 ) ; if ( _spl_scope != 0 ) { Token * last = Nvl ( GetNextCharToken ( ' ' , L' ' ) , GetLastToken ( ) ) ; PrependCopy ( _spl_start , declare , last , false ) ; Prepend ( _spl_start , " \n \n " , L" \n \n " , 2 ) ; Token :: Remove ( declare , last ) ; } if ( no_data == true ) { if ( where_end == NULL ) Append ( from_end , " " , L" " , 10 , declare ) ; else Append ( where_end , " " , L" " , 8 , declare ) ; } if ( select != NULL && options_start != NULL ) { PrependSpaceCopy ( as , options_start , options_end , false ) ; Token :: Remove ( options_start , options_end ) ; } } else if ( _target == SQL_SQL_SERVER ) { Token :: Change ( declare , " " , L" " , 6 ) ; Token :: Remove ( global , temporary ) ; } return true ; }
DECLARE GLOBAL TEMPORARY TABLE in DB2
DECLARE GLOBAL TEMPORARY TABLE in DB2
// Table name // In DB2 temporary table name can include SESSION. qualifier in declaration // Add # before table name in SQL Server // In DB2 if AS keyword is specified next, then (SELECT ...) must follow // Next token must be ( /*Token *open */ // Column definitions or SELECT statement can be specified // Parse colum definitions // Subquery // Next token must be ) /*Token *close */ // Optional ON COMMIT DELETE | PRESERVE ROWS etc. // CREATE GLOBAL TEMPORARY TABLE in Oracle moved outside the procedure // Move CREATE TEMPORARY TABLE definition before CREATE PROCEDURE, CREATE FUNCTION etc. // Definition only, add WHERE 1=0 // No WHERE clause // If SELECT is specified options must be before SELECT in Oracle, not after as in DB2 // CREATE TABLE #name in SQL Server