Teradata自定義函式Replace

miguelmin發表於2010-07-01

Teradata沒有函式replace,為了方便使用,定義了一個。其實也挺簡單的,描述如下:

一、用c編寫一個replace函式,挺簡單,也可以在網上找到原始碼,編譯透過,能達到目的即可。

二、執行一個bteq,將自定義函式載入。

詳細描述如下:

[@more@]

a、編寫teradata建立自定義函式ddl:

REPLACE FUNCTION TEST.treplace (
input_string VARCHAR(16000) CHARACTER SET LATIN,
search_string VARCHAR(512) CHARACTER SET LATIN,
replace_string VARCHAR(512) CHARACTER SET LATIN)
RETURNS VARCHAR(16000) CHARACTER SET LATIN
SPECIFIC replace2LANGUAGE C
NO SQL
PARAMETER STYLE SQL
DETERMINISTIC
CALLED ON NULL INPUT
EXTERNAL NAME 'CS!replace2!replace2.c'
;

其中treplace即為自定義函式名,replace2.c為c程式檔名。

b、將上述檔案儲存為replacebtq(或其他任意你喜歡的名字);

c、執行bteq "logon copid/user,password" < replacebtq

執行成功後treplace函式即可用。注意,在administrator中我們看到的函式名稱為replace2,但真正用的應該是我們定義的function,即treplace。

實驗如下:

select test.treplace('abcdecd','cd','12345') ;

--------------------------------

ab12345e12345

附上我找到的c語言replace,即我使用的函式:

void replace2(VARCHAR_LATIN *input_string,
VARCHAR_LATIN *search_string,
VARCHAR_LATIN *replace_string,
VARCHAR_LATIN *result_string,
int *input_string_indicator,
int *search_string_indicator,
int *replace_string_indicator,
int *result_string_indicator,
char sqlstate[6],
SQL_TEXT extname[129],
SQL_TEXT specific_name[129],
SQL_TEXT error_message[257])
{
int source_length;
int search_length;
int replace_length;
unsigned char *source_ptr;
unsigned char *source_limit;
unsigned char *match_limit;
unsigned char *save_source_ptr;
unsigned char *target_ptr;
unsigned char *target_limit;
unsigned char *search_ptr;
unsigned char *search_limit;


if (*input_string_indicator == IS_NULL)
{
*result_string_indicator = IS_NULL;

(void) strcpy(sqlstate, UDF_OK);
error_message[0] = EOS;

return;
}

source_length = strlen((char *) input_string);

if (*replace_string_indicator == IS_NOT_NULL)
replace_length = strlen((char *) replace_string);
else
replace_length = 0;

/* Don't search if it is impossible to get a match. */

if ((source_length == 0) ||
(*search_string_indicator == IS_NULL) ||
((search_length = strlen((char *) search_string)) == 0) ||
(search_length > source_length))
{
if (source_length > MAXIMUM_LENGTH)
{
*result_string_indicator = IS_NULL;

(void) sprintf(sqlstate, UDF_ERR_RESULTLENGTH1);
(void) strcpy((char *) error_message, UDF_MSG_RESULTLENGTH1);

return;
}

(void) strcpy((char *) result_string, (char *) input_string);

*result_string_indicator = IS_NOT_NULL;

(void) strcpy(sqlstate, UDF_OK);
error_message[0] = EOS;

return;
}

source_ptr = input_string;
source_limit = input_string + source_length;
match_limit = (source_limit - search_length) + 1;
target_ptr = result_string;
target_limit = result_string + MAXIMUM_LENGTH;
search_ptr = search_string;
search_limit = search_string + search_length;

while (source_ptr < source_limit)
{
if ((*search_ptr == *source_ptr) && (source_ptr < match_limit))
{
/* Possible match. Check remainder of pattern */

save_source_ptr = source_ptr;

while ((++search_ptr < search_limit) &&
(*search_ptr == *(++source_ptr)));

if (search_ptr == search_limit)
{
/* Pattern match. */

++source_ptr;

if (replace_length > 0)
{
/* Substitute new value. */

*target_ptr = EOS;

if (((target_ptr + replace_length) - 1) >= target_limit)
{
*result_string_indicator = IS_NULL;

(void) sprintf(sqlstate, UDF_ERR_RESULTLENGTH2);
(void) strcpy((char *) error_message,
UDF_MSG_RESULTLENGTH2);

return;
}

strcat((char *) target_ptr, (char *) replace_string);

target_ptr += replace_length;
}
}
else
{
source_ptr = save_source_ptr;

if (target_ptr == target_limit)
{
*result_string_indicator = IS_NULL;

(void) sprintf(sqlstate, UDF_ERR_RESULTLENGTH3);
(void) strcpy((char *) error_message,
UDF_MSG_RESULTLENGTH3);

return;
}

*(target_ptr++) = *(source_ptr++);
}

search_ptr = search_string;
}
else
{
if (target_ptr == target_limit)
{
*result_string_indicator = IS_NULL;

(void) sprintf(sqlstate, UDF_ERR_RESULTLENGTH4);
(void) strcpy((char *) error_message, UDF_MSG_RESULTLENGTH4);

return;
}

*(target_ptr++) = *(source_ptr++);
}
}

*target_ptr = EOS;

*result_string_indicator = IS_NOT_NULL;

(void) strcpy(sqlstate, UDF_OK);
error_message[0] = EOS;
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16723161/viewspace-1034829/,如需轉載,請註明出處,否則將追究法律責任。

相關文章