cancel
Showing results for 
Search instead for 
Did you mean: 

I need a clr function example

g_g_99
Participant
3,830

I got in c#

// namespace SqlAnywhereDotNetDll

public class Util { public static string toHexStr(Int64 id) { return id.ToString("X2"); } public static string intToHexStr(int id) { return id.ToString("X2"); }

public static int fromHexStr(string h) { return int.Parse(h, System.Globalization.NumberStyles.HexNumber); }

}

and I have tried to interface to toHexStr with the clr function declared as

ALTER FUNCTION "dba"."uidToHexStr"(in @ID bigInt) returns varchar(16) not deterministic external name 'SqlAnywhereDotNetDll.dll::SqlAnywhereDotNetDll.Util.toHexStr( numeric)' language CLR

But it's clearly wrong as I get from isql :

Procedure 'uidToHexStr' terminated with unhandled exception 'Invalid type in parameter list: numeric' SQLCODE=-91, ODBC 3 State="HY000"

with

begin declare @bi bigint; set @bi=9223372036854775807; select dba.uidToHexStr(@bi); end

Note I have tried bigInt, Int64 instead of numeric in the stored function arg type

as a side note, we don't need uidToHexStr nor intToHexStr since SA has a built-in function intToHex that works on bigint and int. I used the clr dll just to try out how to integrate clr function before I proceed to something serious

Accepted Solutions (0)

Answers (1)

Answers (1)

jeff_albion
Product and Topic Expert
Product and Topic Expert

The list of types provided to the external function call in the external name definition are the CLR types:

http://dcx.sybase.com/index.html#sa160/en/dbprogramming/pg-extenv-clr.html

So in this case, you will want:

CREATE FUNCTION "dba"."uidToHexStr"(in id_field bigint)
returns varchar(16)
not deterministic
external name
'SqlAnywhereDotNetDll.dll::SqlAnywhereDotNetDll.Util.toHexStr( long )' language CLR;
g_g_99
Participant

thx, it works after adding string after the )

sorry for not commenting earlier

g_g_99
Participant
0 Kudos

OT: despite only .net 2 is supported for Sql anywhere v 11, .net 3 can do the job as well so far