简体   繁体   English

SQL Server 2008中的正则表达式

[英]Regular expressions in SQL Server 2008

How to write this check constraint: 如何编写此检查约束:

[AB]+ , varchar column with nonempty strings consisting of A's or B's. [AB] + ,varchar列,非空字符串由A或B组成。

Some constraints do work, this simple doesn't. 有些限制确实有效,但这种情况并非如此。
Problem with collation, or something? 整理或其他问题?

SQL server does not natively support Regex. SQL Server本身不支持Regex。

However, it is possible to add check constraint to match the pattern that you provided 但是,可以添加检查约束以匹配您提供的模式

not like '%[^AB]%'

Test: 测试:

declare @Test table(TestColumn varchar(100) check (TestColumn not like '%[^AB]%' and TestColumn != ''))

insert @Test
values ('AABAB')  -- passed

insert @Test
values ('AAB')    -- passed

insert @Test
values ('AABC')   -- failed  

insert @Test
values ('')  -- failed

LIKE patterns are very limited. LIKE模式非常有限。 If you need true Regex constraints you could implement very simple CLR function. 如果您需要真正的Regex约束,您可以实现非常简单的CLR功能。 There are plenty examples in the internet. 互联网上有很多例子。 For example: Regular Expressions Make Pattern Matching And Data Extraction Easier . 例如: 正则表达式使模式匹配和数据提取更容易

SQL server 2008 doesn't support regular expressions natively. SQL Server 2008本身不支持正则表达式。 You can write a custom CLR user-defined function that does this. 您可以编写一个自定义CLR用户定义函数来执行此操作。 MSDN has a wealth of resources to guide you through the process of creating one. MSDN拥有丰富的资源来指导您完成创建过程。 This article , for example. 以这篇文章为例。

Additionally, this article seems to cover exactly what you want to do with check constraints. 此外, 本文似乎准确涵盖了您要对检查约束做什么。 It demonstrates how you can set a CLR UDF as your check constraint. 它演示了如何将CLR UDF设置为检查约束。

If you want to use a full functionality regex in MSSQL constraint - you need to write a custom dll and attach it to the sql server. 如果要在MSSQL约束中使用完整功能正则表达式 - 您需要编写自定义DLL并将其附加到sql server。 then you will able to register it as a local function and use the regext any way you like. 然后你就可以将它注册为本地函数并以任何你喜欢的方式使用regext。

see http://msdn.microsoft.com/en-us/library/w2kae45k(v=vs.80).aspx . 请参阅http://msdn.microsoft.com/en-us/library/w2kae45k(v=vs.80).aspx

If you have diffuculties with this - I can post a simple example of working clr library for MSSQL 如果您对此有困难 - 我可以发布一个为MSSQL工作clr库的简单示例

If using Check constraint given by others above (ie. [AB][AB] etc), does not give you the flexibility to test the varchar value properly then you could try using a trigger. 如果使用上面其他人给出的Check约束(即[AB] [AB]等),则不能让您灵活地正确测试varchar值,那么您可以尝试使用触发器。

A trigger will allow you to test what your after more thoroughly. 触发器将允许您更彻底地测试您的后续内容。

Refer Example C on this link while its using the trigger to test business rules across tables its just a matter of changing it to suit what ever test you need to do :) 使用触发器测试跨表的业务规则时,请参阅此链接上的示例C,只需更改它以适应您需要执行的测试:)

Hope this helps 希望这可以帮助

Sample code to help you along change the testing to suit your needs 示例代码可帮助您更改测试以满足您的需求

CREATE TABLE [dbo].[TestTrigger](
    [stringtest] [varchar](100) NULL
) ON [PRIMARY]
GO

CREATE TRIGGER [dbo].[TestTrigger_TestAB] 
    ON [dbo].[TestTrigger] 
    FOR INSERT,UPDATE
AS 
BEGIN
    SET NOCOUNT ON;

    DECLARE @testString varchar(100)
    Declare @Len as int;
    Declare @SearchPattern as varchar(8000);
    Declare @Result as Int;

    SELECT @testString = stringtest FROM inserted

    Select @Len = Len(@testString);

    While @Len>0
    Begin
        Select @SearchPattern = Isnull(@SearchPattern,'') + '[A-B]';
        Select @Len = @Len -1;
    End

    Select @Result = Case When @testString Like @SearchPattern Then 1 Else 0 End;

    IF (@Result = 0)
    BEGIN
        RAISERROR ('Value entered did not contain only A or B', 16, 1)
        ROLLBACK TRANSACTION
    END
END


SET ANSI_PADDING OFF
GO
SELECT * FROM t
WHERE REPLACE(REPLACE(col, 'A', ''), 'B', '') = ''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM