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

  • 相关阅读:
    hdu 3786 寻找直系亲属
    about the plan
    2013 ACM/ICPC Asia Regional Online —— Warmup1 1005 Balls Rearrangement
    poj 1833 给poj跪了 字典排序
    20170630总结
    20170701总结
    20170628总结
    20170629总结
    20170626总结
    20170627总结
  • 原文地址:https://www.cnblogs.com/surfing/p/3941239.html
Copyright © 2011-2022 走看看