我想在DB2中用C创建一个自定义的TRIM函数TrimChr.
C代码是在C/C++版上搜到的:
#include
#include
#include
#include
#include
#include
//Macro for iendity the blanks
#define isSpaces(ch) (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\b' || ch == '\f' || ch == '\n')
char *TrimChr(char *pszSource)
{
//pointer of the first char of string
char *pszHead = pszSource;
//pointer of the last non-blank char
char *pszLast = &pszSource[strlen(pszSource)-1];
//search the location of the last non-blank char
while (isSpaces(*pszLast))
--pszLast;
*(++pszLast) = '\0';
//search the first non-blank char for left shift the string
for ( ; (*pszHead != '\0') && (pszHead != pszLast); ++pszHead)
{
if (! isSpaces(*pszHead))
{
strcpy(pszSource, pszHead);
break;
}
}
return pszSource;
}
int main(int argc,char *argv[])
{
printf("\"");
printf(argv[1]);
printf("\"\n");
printf("\"");
printf(TrimChr(argv[1]));
printf("\"\n");
}
代码可以编译成功并在LINUX环境下运行的.
但是我想用它变成DB2的一个自定义函数时,老是出问题:
values TrimChr(' aaaa ');
[IBM][CLI Driver][DB2/LINUX] SQL0444N Routine "TRIMCHR" (specific name "SQL080529112348600") is implemented with code in library or path ".../sqllib/function/TrimChr", function "TrimChr" which cannot be accessed. Reason code: "5". SQLSTATE=42724
自定义函数是如此写的:
CREATE FUNCTION TrimChr (VARCHAR(400))
RETURNS VARCHAR(400)
EXTERNAL NAME '/home/db2inst1/sqllib/function/TrimChr!TrimChr'
LANGUAGE C
NULL CALL
PARAMETER STYLE DB2SQL
NO SQL
DETERMINISTIC
NO EXTERNAL ACTION
NOT FENCED;
GRANT EXECUTE ON FUNCTION TRIMCHR(VARCHAR(400)) TO PUBLIC;
请问是什么原因?是编译的问题?还是C函数的问题?