Kiểm tra CLR có được enable hay chưa
EXEC SP_CONFIGURE
hoặc
SELECT * FROM sys.configurations
WHERE name = 'clr enabled'
Dùng đoạn script sau để enable CLR
EXEC SP_CONFIGURE 'show advanced options' , '1';
GO
RECONFIGURE;
GO
EXEC SP_CONFIGURE 'clr enabled' , '1'
GO
RECONFIGURE;
GO
Mở Visual Studio, tạo 1 Class Library project
public class FormatTimeLibrary
{
[Microsoft.SqlServer.Server.SqlFunction()]
public static string CheckFormatTime(string requireTime, string timeSource)
{
string strReturnTime=CheckFormatTimeWithRegularExpression(@"\b\d{1,3}\:\d{1,3}\:\d{1,3}\b", timeSource);
if(strReturnTime.Equals("00:00:00"))
{
strReturnTime = CheckFormatTimeWithRegularExpression(@"\b\d{1,3}\:\d{1,3}\b", timeSource);
}
if (strReturnTime.Equals("00:00:00"))
{
strReturnTime = CheckFormatTimeWithSpeciaCase("FromTime", timeSource);
}
return strReturnTime;
}
}
Tiếp theo, copy DLL vào đường dẫn
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLVN\MSSQL\bin
Và copy thêm 1 phiên bản vào ổ C
Tạo 1 assembly trong SQL Server
create assembly SQLServerFunction from 'C:\FormatTimeLibrary.dll' WITH PERMISSION_SET = SAFE
Cuối cùng, viết lại hàm như 1 function trong SQL Server. Nó chỉ tạo function ở database mà ta đang đứng.
create FUNCTION [dbo].[CheckFormatTime](@timeRequire [nvarchar](255), @timeSource [nvarchar](255))
RETURNS [nvarchar](255) WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [SQLServerFunction].[FormatTimeLibrary].[CheckFormatTime]
Đây là lời gọi hàm
Update tblTestTime set TimeValue=dbo.CheckFormatTime('fromtime', TimeValue)
Tham khảo: http://www.codeproject.com/Articles/37377/SQL-Server-CLR-Functions
Tham khảo: http://www.codeproject.com/Articles/37377/SQL-Server-CLR-Functions
0 comments:
Post a Comment