Teradata自訂函數UDF

來源:互聯網
上載者:User

標籤:var   char   語言   lib   使用者   state   instr   mic   引用   

Teradata支援C語言的自訂函數。具體部署方法如下:

1、如下UDF儲存到檔案中,檔案名稱為chs_instr.udf

/*database syslib;replace FUNCTION chs_instr(srcStr VARCHAR(1024), childStr VARCHAR(64)) RETURNS INTEGER LANGUAGE C NO SQL PARAMETER STYLE TD_GENERAL EXTERNAL NAME chs_instr;sel chs_instr(‘弢1234|‘, ‘|‘);sel index(‘弢1234|‘, ‘|‘);*/

2、如下為UDF定義中引用的C函數,檔案名稱為chs_instr.c

#define SQL_TEXT Latin_Text#include <sqltypes_td.h>#include <string.h>/* Result is 0, if search_str does not apper in source_string               *//*        index, a pos(start at 1) to the firt occurrence of search_str of source_string */void chs_instr(VARCHAR_LATIN *source_string,VARCHAR_LATIN *search_str,  int *result,  char sqlstate[6]){    unsigned char *src = source_string, *sub = search_str, c;  int sublen = strlen(sub);    int slen = strlen(src);    int spos = 0;    *result = 0;    while ( spos <= slen-sublen ) {    if (memcmp(src+spos, sub, sublen) == 0) {       *result = spos+1;       break;     }        c = src[spos++];        if (c > 128) spos++;    }    return;}

3、使用bteq登入資料庫(dbc使用者),指定UDF預設儲存資料庫為syslib。

bteq "logon citic/dbc,dbc" < chs_instr.udf

 

 附錄:memcmp函數是按位元組比較的。

s1,s2為字串時候memcmp(s1,s2,1)就是比較s1和s2的第一個位元組的ascII碼值;
memcmp(s1,s2,n)就是比較s1和s2的前n個位元組的ascII碼值;
如:char *s1="abc";
char *s2="acd";
int r=memcmp(s1,s2,3);
就是比較s1和s2的前3個位元組,第一個位元組相等,第二個位元組比較中大小已經確定,不必繼續比較第三位元組了。所以r=-1.

Teradata自訂函數UDF

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.