zoukankan      html  css  js  c++  java
  • Sql CLR创建一个简单的表值函数

    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:ProjectStudySimpleSqlServerProject1inDebugSqlServerProject1.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
    

      

      

  • 相关阅读:
    js键盘事件以及键盘事件拦截
    GO-&获取地址与*解引用
    Go-map
    第22课
    第21课
    第20课
    基于spring+quartz的分布式定时任务框架
    DeepLearning (三) 预处理:主成分分析与白化
    白化whitening
    如何使用RestTemplate访问restful服务
  • 原文地址:https://www.cnblogs.com/lgx5/p/11102634.html
Copyright © 2011-2022 走看看