SqlClr:创建一个简单的表值函数

1. 创建项目:


2. 添加函数代码:

using System;
using System.Data.Sql;
using Microsoft.SqlServer.Server;
using System.Collections;
using System.Data.SqlTypes;
using System.Diagnostics;

public class TabularEventLog
{
    [SqlFunction(TableDefinition = 
@"logTime datetime
,Message nvarchar(4000)
,Category nvarchar(4000)
,InstanceId bigint",
        Name = "ReadEventLog", FillRowMethodName = "FillRow")]
    public static IEnumerable InitMethod(String logname)
    {
        return new EventLog(logname, Environment.MachineName).Entries;
    }

    public static void FillRow(Object obj, out SqlDateTime timeWritten,
        out SqlChars message, out SqlChars category,
        out long instanceId)
    {
        EventLogEntry eventLogEntry = (EventLogEntry)obj;
        timeWritten = new SqlDateTime(eventLogEntry.TimeWritten);
        message = new SqlChars(eventLogEntry.Message);
        category = new SqlChars(eventLogEntry.Category);
        instanceId = eventLogEntry.InstanceId;
    }
}

3. 脚本:

USE MASTER
GO
sp_configure 'show advanced options',1;
GO
RECONFIGURE;
GO
sp_configure 'clr enabled', 1;
GO
RECONFIGURE;
GO
--表值函数放在 db_study 库上
USE db_study
GO
--删除函数
IF OBJECT_ID('[dbo].[ReadEventLog]') IS NOT NULL
	DROP FUNCTION [dbo].ReadEventLog
GO
--删除程序集
IF EXISTS(SELECT * FROM SYS.ASSEMBLIES WHERE NAME='tvfEventLog') 
	DROP  ASSEMBLY tvfEventLog
GO
--创建程序集, 设置为实际路径, 注意应设置为: UNSAFE
CREATE ASSEMBLY tvfEventLog  FROM'D:\Project\StudySimple\SqlServerProject1\bin\Debug\SqlServerProject1.dll' WITH PERMISSION_SET = UNSAFE
GO
--创建表值函数
CREATE FUNCTION dbo.ReadEventLog(@logname nvarchar(100))
RETURNS TABLE (
	logTime DATETIME
	,Message nvarchar(4000)
	,Category nvarchar(4000)
	,InstanceId BIGINT
)
AS
	EXTERNAL NAME tvfEventLog.TabularEventLog.InitMethod 
GO
--查询
SELECT TOP 10 T.logTime, T.Message, T.InstanceId
FROM dbo.ReadEventLog(N'Security') as T
ORDER BY logTime DESC



参考:http://www.microsoft.com/china/msdn/library/data/sqlserver/bb293147.mspx?mfr=true

此页面做法有问题: https://msdn.microsoft.com/zh-cn/library/ms131103(v=sql.120).aspx


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,MSSQL2008R2本身不支持发送HTTP请求的功能,因此不能通过存储过程直接发送GET请求。你可以考虑使用SQLCLR(即SQL Server Common Language Runtime)来实现发送HTTP请求的功能,步骤如下: 1. 创建一个C#类库项目,编一个类来实现发送HTTP请求的功能。 2. 在该类库项目中添加对System.Net命名空间的引用。 3. 在类中编发送HTTP请求的代码,例如使用HttpWebRequest类来发送请求。 4. 将该类库项目编译为DLL文件。 5. 将该DLL文件上传到MSSQL2008R2数据库服务器上。 6. 在MSSQL2008R2数据库中创建一个SQLCLR存储过程,以调用该DLL文件中的类和方法来发送HTTP请求。 以下是一个简单的示例代码,仅供参考: ``` using System; using System.Data; using System.Data.SqlClient; using System.Data.SqlTypes; using Microsoft.SqlServer.Server; using System.Net; public partial class StoredProcedures { [Microsoft.SqlServer.Server.SqlProcedure] public static void SendHttpRequest(SqlString url) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.ToString()); request.Method = "GET"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); response.Close(); } catch (Exception ex) { SqlContext.Pipe.Send(ex.Message); } } } ``` 在此示例中,我们创建了一个名为SendHttpRequest的SQLCLR存储过程,该存储过程接受一个名为url的输入参数,该参数为SqlString类型,表示要发送的HTTP请求的URL。 在存储过程中,我们使用HttpWebRequest类来创建HTTP请求对象,并设置请求的方法为GET。然后,我们发送请求并获取响应,最后关闭响应流。如果发送HTTP请求发生错误,则将错误消息发送回客户端。 请注意,在使用SQLCLR存储过程时,需要启用CLR集成功能。您可以在MSSQL2008R2数据库中使用以下命令启用CLR集成功能: ``` sp_configure 'clr enabled', 1; RECONFIGURE; ``` 启用CLR集成功能后,您可以创建SQLCLR存储过程并调用它,以实现发送HTTP请求的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值