从 SQL 进行操作系统调用
2009-11-20 00:00:00 来源:WEB开发网清单 8. 将数据写入文件的 C 代码
#include <stdio.h>
#include <string.h>
#include <sqludf.h>
void SQL_API_FN writeFileFromParams(
SQLUDF_VARCHAR *fileName, /* input */
SQLUDF_INTEGER *intCol, /* input */
SQLUDF_VARCHAR *charCol, /* input */
SQLUDF_INTEGER *result, /* output */
/* null indicators */
SQLUDF_NULLIND *fileName_ind, SQLUDF_NULLIND *intCol_ind,
SQLUDF_NULLIND *charCol_ind, SQLUDF_NULLIND *result_ind,
SQLUDF_TRAIL_ARGS_ALL)
{
int rc = 0;
int writtenLen = 0;
char row[60+1] = { '\\0' };
struct scratchMap {
FILE *f;
};
/* map the scratchpad */
struct scratchMap *scratch =
struct scratchMap *scratch =
(struct scratchMap *)SQLUDF_SCRAT->data;
*result_ind = -1;
switch (SQLUDF_CALLT) {
case SQLUDF_FIRST_CALL:
/* open file and store the pointer on the scratchpad */
scratch->f = fopen(fileName, "wb");
if (!scratch->f) {
strcpy(SQLUDF_MSGTX, "Could not open file ");
strncat(SQLUDF_MSGTX, fileName, SQLUDF_MSGTEXT_LEN -
strlen(SQLUDF_MSGTX)-1);
strncpy(SQLUDF_STATE, "38300", SQLUDF_SQLSTATE_LEN);
rc = -1;
goto cleanup;
}
}
case SQLUDF_NORMAL_CALL:
if (*intCol < -999999999) {
/* a number of -1000000000 or smaller needs
11 bytes, which is too long for our format */
sprintf(SQLUDF_MSGTX, "The number %d is too long.",
*intCol);
strncpy(SQLUDF_STATE, "38301", SQLUDF_SQLSTATE_LEN);
rc = -2;
goto cleanup;
}
if (strlen(charCol) > 50) {
/* strings must not be longer than 50 characters */
sprintf(SQLUDF_MSGTX, "The string %s is too long.",
charCol);
strncpy(SQLUDF_STATE, "38302", SQLUDF_SQLSTATE_LEN);
rc = -3;
goto cleanup;
}
/* write the given integer and string */
writtenLen = fprintf(scratch->f, "%10d%-50s", *intCol,
charCol);
if (writtenLen != 60) {
sprintf(SQLUDF_MSGTX, "Did not write 60 bytes "
"(wrote %d).", writtenLen);
strncpy(SQLUDF_STATE, "38303", SQLUDF_SQLSTATE_LEN);
rc = -4;
goto cleanup;
}
*result = 0;
*result_ind = 0;
break;
}
cleanup:
switch (SQLUDF_CALLT) {
case SQLUDF_FIRST_CALL:
if (rc == 0) {
break;
}
/* we must close the file descriptor upon an error in
the FIRST call; so we fall through to the final call
processing below */
case SQLUDF_FINAL_CALL:
case SQLUDF_FINAL_CRA:
/* close the file */
fclose(scratch->f);
fclose(scratch->f);
scratch->f = NULL;
}
}
- ››sql server自动生成批量执行SQL脚本的批处理
- ››sql server 2008亿万数据性能优化
- ››SQL Server 2008清空数据库日志方法
- ››sqlserver安装和简单的使用
- ››SQL Sever 2008 R2 数据库管理
- ››SQL SERVER无法安装成功,sqlstp.log文件提示[未发...
- ››Sql Server中通过父记录查找出所有关联的子记录
- ››SqlServer触发器、存储过程和函数
- ››SQL Server 中的事务(含义,属性,管理)
- ››Sqlite数据库插入和读取图片数据
- ››Sql server 2005拒绝了对对象 'xx表' (数...
- ››Sql server 2005拒绝了对对象 'xx表' (数...
更多精彩
赞助商链接