【问题标题】:SQL Server inject where condition of a column implicitlySQL Server隐式注入列的where条件
【发布时间】:2016-07-29 07:48:27
【问题描述】:

Java 项目包含一个轻量级 SQL 客户端,它为最终用户提供一个 UI 来编写自己的 sql 查询,然后使用 JDBC 从允许的数据源执行并查看 HTML 表中的结果。

现有的零售商数据库包含许多表,其中所有表都具有强制的BUSINESS_UNIT (BU) 键。

用户使用 BU Key 登录时,他们应该只能访问其业务部门。

用户这样写查询

select * from [retail].[transaction] where status='closed' and txn_date='2016-07-26'

在内部它应该添加 BU 键,最终要执行的查询将是

 select * from [retail].[transaction] where BUSINESS_UNIT= ? 
       and status='closed' and txn_date='2016-07-26'

对于简单的查询,我可以包含 `BUSINESS_UNIT=?但是当他们编写具有更多业务表和多个连接的查询时,很难找到 where 子句。

有没有其他方法可以实现这一点。

【问题讨论】:

    标签: java sql-server jdbc


    【解决方案1】:

    您可以在retail.transaction 上创建view

    CREATE VIEW v_transaction AS
    SELECT * from [retail].[transaction]
    where BUSINESS_UNIT='XX' 
    

    然后您可以将所有retail.transaction 引用替换为v_transaction

    例如变换

    select * from [retail].[transaction] where status='closed' and txn_date='2016-07-26'
    

    select * from v_transaction where status='closed' and txn_date='2016-07-26'
    

    这也可以作为带有内联视图的单个查询来完成

    with v_transaction AS (
        SELECT * from [retail].[transaction]
        where BUSINESS_UNIT='XX' 
    )
    select * from v_transaction where status='closed' and txn_date='2016-07-26'
    

    【讨论】:

    • 不适合,我还需要在运行时在你的视图中传递 BUSINESS_UNIT
    • 更新为在查询范围内创建视图,仅限查询
    • XX 不是恒定的先生,我需要在运行时传递它的视图/表/存储过程。对于单个 BU,您的解决方案是合适的,但单个 BU 我不需要通过 BU。考虑一张表中有许多 BUSINESS_UNIT,而我只需要查询一个。
    • 当然 XX 不是常数。你剩下的回答一清二楚
    猜你喜欢
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    • 2013-09-08
    • 2014-06-05
    • 1970-01-01
    • 2021-02-03
    • 2018-08-16
    相关资源
    最近更新 更多