从SQL查询中获取参数名称

Nic*_*net 7 c# regex asp.net asp.net-4.0

后端是PostgreSQL服务器9.1.

我正在尝试构建AdHoc XML报告.报告文件将包含SQL查询,所有这些查询都必须以SELECT语句开头.SQL查询将具有参数.根据相关列的数据类型,这些参数将相应地呈现给用户以提供值.

一个重要的SQL查询:

SELECT * FROM customers
WHERE 
(
    customers.customer_code=@customer_code AND customers.location=@location
    AND customers.type=
    (
        SELECT type from types
        WHERE types.code=@type_code
        AND types.is_active = @type_is_active
    )
    AND customers.account_open_date BETWEEN @start_date AND @end_date
)
OR customers.flagged = @flagged;
Run Code Online (Sandbox Code Playgroud)

我想从查询字符串中获取列名和参数的列表,并将它们放入字符串数组中并稍后处理.

我能够使用以下正则表达式仅匹配参数:

@(?)(?<parameter>\w+)
Run Code Online (Sandbox Code Playgroud)

预期比赛:

customers.customer_code=@customer_code
customers.location=@location
types.code=@type_code
types.is_active = @type_is_active
customers.account_open_date BETWEEN @start_date AND @end_date
customers.flagged = @flagged
Run Code Online (Sandbox Code Playgroud)

如何立即匹配"@Parameter","="和"BETWEEN"?

eve*_*ton 3

我知道有点晚了,但为了未来的研究:

我认为这个正则表达式符合您的目的:

(\w+\.\w+(?:\s?\=\s?\@\w+|\sBETWEEN\s\@\w+\sAND\s\@\w+))
Run Code Online (Sandbox Code Playgroud)

在这里检查这个 Regex101 小提琴,并仔细阅读它每个部分的解释。

基本上,它首先查找您的customer.xxx_yyy列,然后查找 for= @variableBETWEEN @variable1 AND @variable2

捕获的组:

MATCH 1
1.  [37-75] 
`customers.customer_code=@customer_code`

MATCH 2
1.  [80-108]    
`customers.location=@location`

MATCH 3
1.  [184-205]   
`types.code=@type_code`

MATCH 4
1.  [218-251]   
`types.is_active = @type_is_active`

MATCH 5
1.  [266-327]   
`customers.account_open_date BETWEEN @start_date AND @end_date`

MATCH 6
1.  [333-361]   
`customers.flagged = @flagged`
Run Code Online (Sandbox Code Playgroud)