zoukankan      html  css  js  c++  java
  • SqlServer中使用正则表达式

    一、查看配置,如果‘show advanced options’,‘Ole Automation Procedures’,‘Ad Hoc Distributed Queries’的config_value和run_value不为1,需要执行二中脚本。

    二、执行脚本,打开SQL Server 对组件的访问阻止。

    USE master
    GO
    sp_configure 'show advanced options', 1;
    GO
    RECONFIGURE;
    GO
    sp_configure 'Ole Automation Procedures', 1;
    GO
    RECONFIGURE;
    GO
    sp_configure 'Ad Hoc Distributed Queries', 1;
    GO
    RECONFIGURE;
    GO

    三、创建函数(除数据库名和函数名外其他保持不变即可)

    Use EDRMSOfficeDB;--(数据库名)
    GO
    if object_id(N'dbo.Reg') is not null --(函数名)
    drop function dbo.Reg;
    GO
    CREATE FUNCTION Reg
    (
    @pattern varchar(2000),
    @matchstring varchar(8000)
    )
    returns int
    as
    begin
    declare @objRegexExp int
    declare @strErrorMessage varchar(255)
    declare @hr int,@match bit
    exec @hr= sp_OACreate 'VBScript.RegExp', @objRegexExp out
    if @hr = 0
    exec @hr= sp_OASetProperty @objRegexExp, 'Pattern', @pattern
    if @hr = 0
    exec @hr= sp_OASetProperty @objRegexExp, 'IgnoreCase', 1
    if @hr = 0
    exec @hr= sp_OAMethod @objRegexExp, 'Test', @match OUT, @matchstring
    if @hr <>0
    begin
    return null
    end
    exec sp_OADestroy @objRegexExp
    return @match
    end

    四、测试

    查询字段中含有出字母和数字之外的记录:

    SELECT CategoryId FROM Category WHERE dbo.Reg('^[A-Za-z0-9]+$',CategoryId )=0

  • 相关阅读:
    UIPickerView-一.01-
    闭包-01-Swift
    Swift 入门-01-概述
    git使用命令行-01-自己操作的
    FetchedResultsController-03-CoreData相关
    SQLite-05-增删改查
    Sqlite函数总结-04
    List<string>转xml
    比较两个List<T>是否相同
    获取DataTable前几条数据
  • 原文地址:https://www.cnblogs.com/surfing/p/3941239.html
Copyright © 2011-2022 走看看