Sql Server 使用正則表達式


 

create function dbo.RegexMatch (    
 
@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 * FROM table where dbo.RegexMatch('正則表達式',字段名)=0

如果執行報錯 執行這個語句

--開啟 Ole Automation Procedures
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
EXEC sp_configure 'Ole Automation Procedures';
GO

--關閉 Ole Automation Procedures
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
EXEC sp_configure 'Ole Automation Procedures';
GO

--關閉高級選項
sp_configure 'show advanced options', 0;
GO
RECONFIGURE;
GO

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM