Showing posts with label udf. Show all posts
Showing posts with label udf. Show all posts

Wednesday, March 21, 2012

Is it possible to create a UDF from a .NET 2.0 assembly that uses dllimport to call unmanaged co

I have been reading up on everything I can find on this subject and I am not clear if this is allowed within the SQL Server 2005 CLR. My function calls work within a Windows form project, but don't run when invoked from a SQL function. I don't get any errors or warnings when creating the assembly and functions within SQL Server, but when running via a select statement, the spid just hangs and I have to stop & restart the service to kill the process. I have been investigating the security settings for this assembly, but I think I have that covered via the RunTime Security Policy settings in the .NET Framework 2.0 Configuration tool.

Any insights, knowledge, or thoughts would be greatly appreciated.

Barry

Hi Barry,

Calling into a CLR function which in turn p-invokes into unmanaged code should work without issues, although we would usually recommend against this if you can avoid it, as it would require the assembly being registered as UNSAFE.

What is the unmanaged code doing? Is it possible to have a look at your SQL function?

Thanks!

-Mat

sql

Is it possible to create a function from within a stored procedure?

I would like to create an UDF from withing a stored procedure. Is it possible?
Thanks a lot.Originally posted by EMoscosoCam
I would like to create an UDF from withing a stored procedure. Is it possible?

Thanks a lot.
try something like this:

declare @.S varchar(1000)

select @.S='
CREATE FUNCTION dbo.fn_test
(
@.p1 int,
@.P2 int
)
RETURNS int
AS
BEGIN
DECLARE @.sum AS int
SELECT @.sum = @.p1 + @.P2
RETURN @.sum
END'
exec(@.S)
go
select dbo.fn_test(5,2)|||Thank you very much! it worked just fine.