?
Solved

How to make Regex work in SQL 2005?

Posted on 2007-10-18
10
Medium Priority
?
2,315 Views
Last Modified: 2008-01-09
Hi I need to use Regex function in SQL 2005, but I got the following message after I ran the query like
select ROUTINE_NAME
from INFORMATION_SCHEMA.ROUTINES
where ROUTINE_TYPE = N'PROCEDURE'
    and dbo.RegexMatch( ROUTINE_NAME,
        N'^usp_(Insert|Update|Delete|Select)([A-Z][a-z]+)+$' ) = 0
-------------------------------------------------------------------------------------------------------------------------------
Msg 4121, Level 16, State 1, Line 1
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.RegexMatch", or the name is ambiguous.
-----------------------------------------------------
So sth is missing? I have to install CLR? How to do that? I was following the article below.
http://msdn.microsoft.com/msdnmag/issues/07/02/SQLRegex/default.aspx

Thanks a lot!
0
Comment
Question by:qinyan
  • 6
  • 2
  • 2
10 Comments
 
LVL 27

Expert Comment

by:ddrudik
ID: 20104552
I suspect you mean to use Regex.Match but that's just a guess.
0
 

Author Comment

by:qinyan
ID: 20104694
In general I want to use the Regular Expression funtion in SQL 2005. For now I can say it's right, I want to use Regex.Match function. But in order to use it i have to first compile the code in Figure 1 from the article and then use create an assembly right? I'm not a .net developer so I don't know how to do that. I just want to know the steps as the article already had the code out there so I want to test it.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 20104724
Can't help with that, sorry.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 30

Accepted Solution

by:
nmcdermaid earned 2000 total points
ID: 20106373
I beleive the process is:

1. Compile your .Net code to a DLL
2. Create an assembly refencining the DLL, using CREATE ASSEMBLY
3. Create a function called RegexMatch which references the assembly

 

This help reference:

ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/864b393f-225f-4895-8c8d-4db59ea60032.htm

Has this example of how to create a CLR UDF:


DECLARE @SamplesPath nvarchar(1024);
-- You may have to modify the value of the this variable if you have
--installed the sample someplace other than the default location.
SELECT @SamplesPath = REPLACE(physical_name, 'Microsoft SQL Server\MSSQL.1\MSSQL\DATA\master.mdf', 'Microsoft SQL Server\90\Samples\Engine\Programmability\CLR\')
    FROM master.sys.database_files
    WHERE name = 'master';

CREATE ASSEMBLY [SurrogateStringFunction]
FROM @SamplesPath + 'StringManipulate\CS\StringManipulate\bin\debug\SurrogateStringFunction.dll'
WITH PERMISSION_SET = EXTERNAL_ACCESS;
GO

CREATE FUNCTION [dbo].[len_s] (@str nvarchar(4000))
RETURNS bigint
AS EXTERNAL NAME [SurrogateStringFunction].[Microsoft.Samples.SqlServer.SurrogateStringFunction].[LenS];
GO


0
 

Author Comment

by:qinyan
ID: 20108481
Thanks very much for the help!
I ran the create assembly statement below but got this message:
Msg 6501, Level 16, State 7, Line 2
CREATE ASSEMBLY failed because it could not open the physical file "C:\Temp\CLR\ClassLibrary1\bin\Release\ClassLibrary1.dll": 3(The system cannot find the path specified.).
-------------------------------------------------------------------------------------------------------------------------
CREATE ASSEMBLY MyRegexTest
FROM 'C:\Temp\CLR\ClassLibrary1\bin\Release\ClassLibrary1.dll'    
WITH PERMISSION_SET = EXTERNAL_ACCESS;
----------------------------------------------------------------------------------------------------------------
so where is the problem? Does the path have to be under Program Files\Microsoft SQL Server...?
Thanks again!
0
 

Author Comment

by:qinyan
ID: 20108691
oK I figured it out the c drive is on my local and the sql server is a remote server. Anyway I was able to create the assembly and I'm testing the function...I'll see how it works. Thanks!
0
 

Author Comment

by:qinyan
ID: 20108894
Well, when I create the function I got this message:
Msg 6505, Level 16, State 1, Procedure RegexMatch, Line 2
Could not find Type 'Microsoft.Samples.SqlServer.MyRegexTest' in assembly 'ClassLibrary1'.
--------------------------------------------------------------------------------------------------------------------------
CREATE FUNCTION [dbo].[RegexMatch] (@str nvarchar(4000))
RETURNS bigint
AS EXTERNAL NAME [MyRegexTest].[Microsoft.Samples.SqlServer.MyRegexTest].[RegexMatch];
GO
0
 

Author Comment

by:qinyan
ID: 20108925
This is the .net code I compiled to dll:
-------------------------------------------------------------------------------------
public static partial class UserDefinedFunctions
{
    public static readonly RegexOptions Options =
        RegexOptions.IgnorePatternWhitespace |
        RegexOptions.Singleline;

    [SqlFunction]
    public static SqlBoolean RegexMatch(
        SqlChars input, SqlString pattern)
    {
        Regex regex = new Regex( pattern.Value, Options );
        return regex.IsMatch( new string( input.Value ) );
    }
}
-----------------------------------------------------------------------------
And I was trying to test the function by running this query see whether the input string matches the pattern specified.
select dbo.RegexMatch( N'123-45-6789', N'^\d{3}-\d{2}-\d{4}$' )

Thank you!
0
 

Author Comment

by:qinyan
ID: 20109244
OK, OK, I figured it out:
CREATE FUNCTION [dbo].[RegexMatch] (@str nvarchar(4000), @pattern nvarchar(4000))
RETURNS bit
AS EXTERNAL NAME [MyRegexTest].[ClassLibrary1.UserDefinedFunctions].[RegexMatch];
GO

then the query below returns 1.
select dbo.RegexMatch( N'123-45-6789', N'^\d{3}-\d{2}-\d{4}$' )


Thanks a lot!
0
 
LVL 30

Expert Comment

by:nmcdermaid
ID: 20113932
Excellent, now we all know how to do it! :)
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are some very powerful Dynamic Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a di…
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question