1、首先编写下面的代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <fcntl.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> int strtime2num(char *tstr) { struct tm m; if (tstr==NULL) return 0; m.tm_year = atoi(tstr)-1900; m.tm_mon = atoi(tstr+5)-1; m.tm_mday = atoi(tstr+8); m.tm_hour = atoi(tstr+11); m.tm_min = atoi(tstr+14); m.tm_sec = atoi(tstr+17); return(mktime(&m)); }
编译选项,solaris下的cc编译,其他平台可以自行处理:
test_func.o : test_func.c
cc -g -xarch=v9 -o ${SYNCSVRDIR}/test_func.o -c $(SYNCSVRDIR)/test_func.c
test_func.so: test_func.o
cc -G -Kpic -xarch=v9 -o test_func.so test_func.o
关键记得把这个so放到server运行的机器上去,比方说放到:/database/oracle/product/10.2.0/Db_1/lib/test_func.so。在oracle里面创建函数:
create or replace library MYLIB as '/database/oracle/product/10.2.0/Db_1/lib/test_func.so';
CREATE OR REPLACE FUNCTION mytest1(a varchar2) RETURN binary_integer AS
LANGUAGE C NAME "strtime2num"
LIBRARY MYLIB PARAMETERS (a STRING,RETURN int);
好了,试试:
select mytest1('2006-12-01 00:00:00') from dual;