本文发表在 rolia.net 枫下论坛steps:
1. write an external UDF and compile:
struct scalar_scratchpad_data
{
int counter;
int cid;
};
void SQL_API_FN ScratchpadScUDF(SQLUDF_INTEGER *inId,
SQLUDF_INTEGER *outCounter,
SQLUDF_SMALLINT *inNullInd,
SQLUDF_SMALLINT *counterNullInd,
SQLUDF_TRAIL_ARGS_ALL)
{
struct scalar_scratchpad_data *pScratData;
pScratData = (struct scalar_scratchpad_data2 *)SQLUDF_SCRAT->data;
switch (SQLUDF_CALLT)
{
case SQLUDF_FIRST_CALL:
pScratData->counter = 1;
pScratData->cid = *inId;
break;
case SQLUDF_NORMAL_CALL:
if (*inId == pScratData->cid)
{
pScratData->counter = pScratData->counter + 1;
}
else
{
pScratData->cid = *inId;
pScratData->counter = 1;
}
break;
case SQLUDF_FINAL_CALL:
break;
}
*outCounter = pScratData->counter;
*counterNullInd = 0;
}
2. define such UDF:
drop FUNCTION ScratchpadScUDF(int);
CREATE FUNCTION ScratchpadScUDF(int) RETURNS INTEGER EXTERNAL NAME 'udfsrv!ScratchpadScUDF' FENCED SCRATCHPAD 100 FINAL CALL VARIANT NO SQL PARAMETER STYLE DB2SQL LANGUAGE C NO EXTERNAL ACTION;
3.
SELECT ScratchpadScUDF(id),id, col1, col2, col3 FROM tbl order by id;
1 ID COL1 COL2 COL3
----------- ----------- ---- ---- ----
1 0 x5 y2 z7
2 0 x5 y4 z3
3 0 x7 y4 z7
4 0 x8 y8 z7
5 0 x2 y0 z5
6 0 x0 y8 z1
7 0 x7 y8 z7
8 0 x1 y1 z1
9 0 x1 y1 z8
10 0 x8 y5 z4
1 1 x8 y6 z4
2 1 x7 y5 z2
3 1 x4 y6 z4
4 1 x9 y0 z2
5 1 x4 y5 z3
6 1 x6 y9 z8
7 1 x1 y0 z1
8 1 x0 y3 z1
9 1 x1 y7 z6
10 1 x6 y0 z9
11 1 x4 y3 z0
12 1 x2 y1 z3
1 2 x3 y0 z1
2 2 x1 y9 z4
3 2 x2 y3 z2
4 2 x4 y1 z4
5 2 x6 y6 z9
6 2 x5 y1 z6
7 2 x1 y5 z9
8 2 x3 y4 z7
9 2 x1 y6 z1
10 2 x6 y8 z7更多精彩文章及讨论,请光临枫下论坛 rolia.net
1. write an external UDF and compile:
struct scalar_scratchpad_data
{
int counter;
int cid;
};
void SQL_API_FN ScratchpadScUDF(SQLUDF_INTEGER *inId,
SQLUDF_INTEGER *outCounter,
SQLUDF_SMALLINT *inNullInd,
SQLUDF_SMALLINT *counterNullInd,
SQLUDF_TRAIL_ARGS_ALL)
{
struct scalar_scratchpad_data *pScratData;
pScratData = (struct scalar_scratchpad_data2 *)SQLUDF_SCRAT->data;
switch (SQLUDF_CALLT)
{
case SQLUDF_FIRST_CALL:
pScratData->counter = 1;
pScratData->cid = *inId;
break;
case SQLUDF_NORMAL_CALL:
if (*inId == pScratData->cid)
{
pScratData->counter = pScratData->counter + 1;
}
else
{
pScratData->cid = *inId;
pScratData->counter = 1;
}
break;
case SQLUDF_FINAL_CALL:
break;
}
*outCounter = pScratData->counter;
*counterNullInd = 0;
}
2. define such UDF:
drop FUNCTION ScratchpadScUDF(int);
CREATE FUNCTION ScratchpadScUDF(int) RETURNS INTEGER EXTERNAL NAME 'udfsrv!ScratchpadScUDF' FENCED SCRATCHPAD 100 FINAL CALL VARIANT NO SQL PARAMETER STYLE DB2SQL LANGUAGE C NO EXTERNAL ACTION;
3.
SELECT ScratchpadScUDF(id),id, col1, col2, col3 FROM tbl order by id;
1 ID COL1 COL2 COL3
----------- ----------- ---- ---- ----
1 0 x5 y2 z7
2 0 x5 y4 z3
3 0 x7 y4 z7
4 0 x8 y8 z7
5 0 x2 y0 z5
6 0 x0 y8 z1
7 0 x7 y8 z7
8 0 x1 y1 z1
9 0 x1 y1 z8
10 0 x8 y5 z4
1 1 x8 y6 z4
2 1 x7 y5 z2
3 1 x4 y6 z4
4 1 x9 y0 z2
5 1 x4 y5 z3
6 1 x6 y9 z8
7 1 x1 y0 z1
8 1 x0 y3 z1
9 1 x1 y7 z6
10 1 x6 y0 z9
11 1 x4 y3 z0
12 1 x2 y1 z3
1 2 x3 y0 z1
2 2 x1 y9 z4
3 2 x2 y3 z2
4 2 x4 y1 z4
5 2 x6 y6 z9
6 2 x5 y1 z6
7 2 x1 y5 z9
8 2 x3 y4 z7
9 2 x1 y6 z1
10 2 x6 y8 z7更多精彩文章及讨论,请光临枫下论坛 rolia.net