[英]Getting error when using UDF with SQL Query
通过使用SQL项目模板将Visual Studio中的SQL项目模板用于Visual Studio,我已经做出了CLR:
[Microsoft.SqlServer.Server.SqlFunction()]
public static SqlDateTime ScalarUDF(SqlInt64 CompanyID)
{
SqlInt64 temp = CompanyID;
string zoneId = "Singapore Standard Time";
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(zoneId);
DateTime result = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tzi);
return new SqlDateTime(result);
}
并且还使用查询将UDF创建为SQL:
-- Install Assembly
CREATE ASSEMBLY UDF_Trial FROM 'C:\Users\Rahul\Documents\visual studio 2013\Projects\UDF_Trial\UDF_Trial\bin\Debug\UDF_Trial.dll'
GO
-- Create ScalarUDF
CREATE FUNCTION [dbo].[ScalarUDF](@CompanyID bigint)
RETURNS datetime
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME UDF_Trial.UserDefinedFunctions.ScalarUDF;
GO
但是在执行UDF时,我会遇到一些错误:
我正在执行UDF,
select [dbo].[ScalarUDF](15)
但是获取异常为:
Msg 6522, Level 16, State 2, Line 2
A .NET Framework error occurred during execution of user-defined routine or aggregate "ScalarUDF":
System.Security.HostProtectionException: Attempted to perform an operation that was forbidden by the CLR host.
The protected resources (only available with full trust) were: All
The demanded resources were: MayLeakOnAbort
System.Security.HostProtectionException:
at UserDefinedFunctions.ScalarUDF(SqlInt64 CompanyID)
.
创建大会时,我还向UNSAFE
授予了以下权限:
使用PERMISSION_SET = UNSAFE从'C:\\ Users \\ Rahul \\ Desktop \\ pro_temp \\ UDF_Trial.dll'创建组装UDF_Trial; 走
但是在这种情况下,我得到的错误为:
CREATE ASSEMBLY for assembly 'UDF_Trial' failed because assembly 'UDF_Trial' is not authorized for PERMISSION_SET = UNSAFE. The assembly is authorized when either of the following is true: the database owner (DBO) has UNSAFE ASSEMBLY permission and the database has the TRUSTWORTHY database property on; or the assembly is signed with a certificate or an asymmetric key that has a corresponding login with UNSAFE ASSEMBLY permission.
我将数据库的可信任度设置为:
ALTER DATABASE Learn SET TRUSTWORTHY ON
并将Assembly创建为::
CREATE ASSEMBLY UDF_Blog FROM 'C:\Users\Rahul\Documents\visual studio 2013\Projects\UDF_Trial\UDF_Trial\bin\Debug\UDF_Trial.dll'
WITH PERMISSION_SET = UNSAFE;
GO
问题就解决了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.