Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mssql的子查询报错 #2744

Open
shixy163 opened this issue Jul 30, 2024 · 0 comments
Open

mssql的子查询报错 #2744

shixy163 opened this issue Jul 30, 2024 · 0 comments

Comments

@shixy163
Copy link

重现步骤

当mssql查询语句中带有子查询时,拼接的sql语句会将子查询中的select也增加top限制,导致子查询结果错误

--原始sql
select * from table where id in (select id from table2)
--生成的sql
select top 100 * from table where id in (select top 100 id from table2)

错误代码位置:
sql/engines/mssql.py

def filter_sql(self, sql="", limit_num=0):
        sql_lower = sql.lower()
        # 对查询sql增加limit限制
        if re.match(r"^select", sql_lower):
            if sql_lower.find(" top ") == -1:
                if sql_lower.find(" distinct ") > 0:
                    return sql_lower.replace(
                        "distinct", "distinct top {}".format(limit_num)
                    )
                return sql_lower.replace("select", "select top {}".format(limit_num)) 
                # 这里替换的时候会连通子查询也进行替换,导致查询结果错误
                return sql_lower.replace("select", "select top {}".format(limit_num),1) 
        return sql.strip()

预期外的结果

--原始sql
select * from table where id in (select id from table2)
--生成的sql
select top 100 * from table where id in (select id from table2)

日志文本

No response

版本

v1.9.1

部署方式

Docker

是否还有其他可以辅助定位问题的信息?比如数据库版本等

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant