简体   繁体   English

sql 服务器如何在具有不同返回结果的多个查询中重用 where 条件

[英]sql server how to reuse where conditions in multiple queries with different returning results

I have a huge sql query with multiple joins and bundle of where conditions (filters).我有一个巨大的 sql 查询,其中包含多个连接和捆绑的 where 条件(过滤器)。 The query returns results with pagination or page wise.查询返回分页或分页结果。 Now in other case I need only a total count as result instead of records list based on same applied filters.现在在其他情况下,我只需要一个总计数作为结果,而不是基于相同应用过滤器的记录列表。 One way is to repeat/copy the same query and changing the SELECT clause plus removing the pagination OFFSET @PageSize * (@PageIndex - 1) ROWS FETCH NEXT @PageSize ROWS ONLY and all the WHERE conditions would be repeated, so in future if there's any change I have to change both queries.一种方法是重复/复制相同的查询并更改SELECT子句并删除分页OFFSET @PageSize * (@PageIndex - 1) ROWS FETCH NEXT @PageSize ROWS ONLY并且所有 WHERE 条件都将重复,所以将来如果有任何更改我都必须更改两个查询。 Is there any way around to reuse all the WHERE conditions since only the selection is being changed?有什么办法可以重用所有 WHERE 条件,因为只有选择被更改?

First query:第一个查询:

SELECT Id,Name
FROM Users
WHERE 1=1
...bundle of where conditions
ORDER BY Name
OFFSET @PageSize * (@PageIndex - 1) ROWS FETCH NEXT @PageSize ROWS ONLY

Second query:第二个查询:

SELECT COUNT(1)
FROM Users
WHERE 1=1
...reuse above bundle of where conditions

Two options spring to mind.想到两个选项 spring。

Dynamic SQL is your friend here but a more pragmatic design using Views will allow you to centralise the where clauses....动态 SQL 在这里是您的朋友,但使用视图的更实用的设计将允许您集中 where 子句....

If you need to adjust the WHERE clauses you simply change the view implementation, this will allow to cascade the updates everywhere the view is used.如果您需要调整 WHERE 子句,您只需更改视图实现,这将允许在使用视图的任何地方级联更新。

Create View vwCustomUsers
SELECT Id,Name
FROM Users
WHERE 1=1
...bundle of where conditions


SELECT Id,Name
FROM vwCustomUsers
WHERE 1=1
ORDER BY Name
OFFSET @PageSize * (@PageIndex - 1) ROWS FETCH NEXT @PageSize ROWS ONLY

SELECT COUNT(1)
FROM vwCustomUsers
WHERE 1=1

If it is relevant to you, you can do it two ways I can think of如果它与你有关,你可以用我能想到的两种方式来做

I didn't test, but query would be like this:我没有测试,但查询会是这样的:

  1. With store procedure: create a store procedure with parameters, while querying, store store procedure out in a temp table and join with other table使用存储过程:创建带参数的存储过程,查询时将存储过程存储在临时表中并与其他表连接

  2. Create a prameterized function and you can cross apply with your other tables.创建一个参数化的 function,您可以与其他表cross apply

1. For store proredure 1. 用于存储过程

Suppose our store procedure name is SelectFilteredUsers .假设我们的存储过程名称是SelectFilteredUsers

CREATE PROCEDURE SelectUsersWithFilter @param1 @param2
AS
SELECT * FROM Users WHERE --- --- ---
GO;

Later query would be稍后的查询将是

INSERT INTO #Temp
EXEC SelectFilteredUsers(@param1, @param2)

SELECT Id,Name
FROM #Temp
WHERE 1=1
ORDER BY Name
OFFSET @PageSize * (@PageIndex - 1) ROWS FETCH NEXT @PageSize ROWS ONLY

2. With functions 2.具有功能

CREATE FUNCTION dbo.SelectUsersWithFilter (@params)
RETURNS @retFindReports TABLE .....

and you can do like你可以这样做

SELECT Id,Name
FROM dbo.fn_MarketDataDetails (@params)
ORDER BY Name
OFFSET @PageSize * (@PageIndex - 1) ROWS FETCH NEXT @PageSize ROWS ONLY

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

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