Teradata Custom Function UDF

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.