qinyan
asked on
How to make Regex work in SQL 2005?
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.ROUTINE S
where ROUTINE_TYPE = N'PROCEDURE'
and dbo.RegexMatch( ROUTINE_NAME,
N'^usp_(Insert|Update|Dele te|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!
select ROUTINE_NAME
from INFORMATION_SCHEMA.ROUTINE
where ROUTINE_TYPE = N'PROCEDURE'
and dbo.RegexMatch( ROUTINE_NAME,
N'^usp_(Insert|Update|Dele
--------------------------
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!
I suspect you mean to use Regex.Match but that's just a guess.
ASKER
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.
Can't help with that, sorry.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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\Relea se\ClassLi brary1.dll ": 3(The system cannot find the path specified.).
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -----
CREATE ASSEMBLY MyRegexTest
FROM 'C:\Temp\CLR\ClassLibrary1 \bin\Relea se\ClassLi brary1.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!
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
--------------------------
CREATE ASSEMBLY MyRegexTest
FROM 'C:\Temp\CLR\ClassLibrary1
WITH PERMISSION_SET = EXTERNAL_ACCESS;
--------------------------
so where is the problem? Does the path have to be under Program Files\Microsoft SQL Server...?
Thanks again!
ASKER
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!
ASKER
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.SqlServ er.MyRegex Test' in assembly 'ClassLibrary1'.
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ------
CREATE FUNCTION [dbo].[RegexMatch] (@str nvarchar(4000))
RETURNS bigint
AS EXTERNAL NAME [MyRegexTest].[Microsoft.S amples.Sql Server.MyR egexTest]. [RegexMatc h];
GO
Msg 6505, Level 16, State 1, Procedure RegexMatch, Line 2
Could not find Type 'Microsoft.Samples.SqlServ
--------------------------
CREATE FUNCTION [dbo].[RegexMatch] (@str nvarchar(4000))
RETURNS bigint
AS EXTERNAL NAME [MyRegexTest].[Microsoft.S
GO
ASKER
This is the .net code I compiled to dll:
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------
public static partial class UserDefinedFunctions
{
public static readonly RegexOptions Options =
RegexOptions.IgnorePattern Whitespace |
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!
--------------------------
public static partial class UserDefinedFunctions
{
public static readonly RegexOptions Options =
RegexOptions.IgnorePattern
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!
ASKER
OK, OK, I figured it out:
CREATE FUNCTION [dbo].[RegexMatch] (@str nvarchar(4000), @pattern nvarchar(4000))
RETURNS bit
AS EXTERNAL NAME [MyRegexTest].[ClassLibrar y1.UserDef inedFuncti ons].[Rege xMatch];
GO
then the query below returns 1.
select dbo.RegexMatch( N'123-45-6789', N'^\d{3}-\d{2}-\d{4}$' )
Thanks a lot!
CREATE FUNCTION [dbo].[RegexMatch] (@str nvarchar(4000), @pattern nvarchar(4000))
RETURNS bit
AS EXTERNAL NAME [MyRegexTest].[ClassLibrar
GO
then the query below returns 1.
select dbo.RegexMatch( N'123-45-6789', N'^\d{3}-\d{2}-\d{4}$' )
Thanks a lot!
Excellent, now we all know how to do it! :)