Teradata supports C-language custom functions. The specific deployment methods are as follows:
1, the following UDF stored in the file, the file name is called chs_instr.udf
/* database Syslib;replace FUNCTION chs_instr (srcstr varchar (1024x768), childstr varchar) RETURNS INTEGER LANGUAGE C NO S QL PARAMETER STYLE td_general EXTERNAL NAME chs_instr;sel chs_instr (' Mingtao 1234| ', ' | '); Sel index (' Mingtao 1234| ', ' | '); */
2, the following is the C function referenced in the UDF definition, the file name is called CHS_INSTR.C
#defineSql_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*/voidCHS_INSTR (Varchar_latin *source_string,varchar_latin *Search_str,int*result,Charsqlstate[6]) {unsignedChar*SRC = source_string, *sub =Search_str, C; intSublen =strlen (sub); intSlen =strlen (SRC); intSpos =0; *result =0; while(Spos <= slen-Sublen) { if(MEMCMP (Src+spos, sub, sublen) = =0) { *result = spos+1; Break; } C= src[spos++]; if(C > -) spos++; } return;}
3. Use the Bteq login database (DBC user) to specify that the UDF default storage database is Syslib.
" logon Citic/dbc,dbc " chs_instr.udf
Appendix: The MEMCMP function is compared by byte.
S1,S2 is a string when memcmp (s1,s2,1) is the first byte of the comparison S1 and S2 ASCII code value;
MEMCMP (s1,s2,n) is the ASCII code value comparing the first n bytes of S1 and S2;
such as: Char *s1= "ABC";
Char *s2= "ACD";
int r=memcmp (s1,s2,3);
is to compare the first 3 bytes of S1 and S2, the second byte is equal, the size of the next byte comparison is determined, and you do not have to continue comparing the third byte. So r=-1.
Teradata Custom Function UDF