在对结果进行分组之前动态声明FILTER条件 [英] Dynamically declare the FILTER condition before grouping the results
本文介绍了在对结果进行分组之前动态声明FILTER条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请建议
有这样的SELECT功能
问题是动态在GROUP
之前声明WHERE条件,并将此请求放在服务器端,如TVF或View
...或者这可以通过其他更好的方法实现?
Please suggest
there is such SELECT function
an issue is to dynamically declare the WHERE condition before GROUP
and to place this request on the server side like TVF or View
...or this can be implemented by some other better approach?
SELECT lt.PNPart, mg.PartNumber
FROM LinkTable AS lt INNER JOIN
mfu AS mg ON lt.PNPart = mg.PartNumber
where PrintColorMono =2 or PrintSpeedA4Simplex = 25 or PrintResolutionMax = 5 or DevicePosition = 2 or Manufacturer = 1
GROUP BY lt.PNPart, mg.PartNumber
What I have tried:
T-SQL documentation, MSDN, Forums
推荐答案
快速的方法是使用存储过程并传入一个参数(变量)来指示过程哪个查询通过 IF
语句执行。
The quick way would be to use a stored procedure and pass in a parameter(variable) to instruct the procedure which query to execute via IF
statements.
CREATE PROCEDURE dbo.MySProc
(@QueryOption INT)
AS
BEGIN
IF (@QueryOption = 1) BEGIN
-- TSQL to execute for selected option
END
ELSE IF (@QueryOption = 2) BEGIN
-- TSQL to execute for selected option
END
-- and so on
END
GO
然后,您可以使用参数调用此存储过程,告诉您要运行哪个语句
You would then call this Stored Procedure with with a parameter telling you which statement you want to run
EXECUTE dbo.MySProc @QueryOption =1
这篇关于在对结果进行分组之前动态声明FILTER条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文