开发者

select comparison operator on basis of parameter

开发者 https://www.devze.com 2023-01-31 20:22 出处:网络
I want to compare columns on the basis of parameter e.g. CREATE PROCEDURE [dbo].[usp_GetTenderBySearch]

I want to compare columns on the basis of parameter

e.g.

  CREATE PROCEDURE [dbo].[usp_GetTenderBySearch]
  (
      p_strOperator char(1),
    p_dtClosingDate datetime
  )
  AS

  Select * From MyTable
  Where Case p_strOperator
        When '=' Then MyColumn = p开发者_StackOverflow中文版_dtClosingDate
        When '>' Then MyColumn > p_dtClosingDate
        When '<' Then MyColumn < p_dtClosingDate
  End

Any suggestions?


How about this:

SELECT *
  FROM MyTable
 WHERE (p_strOperator = '=' AND MyColumn = p_dtClosingDate)
    OR (p_strOperator = '>' AND MyColumn > p_dtClosingDate)
    OR (p_strOperator = '<' AND MyColumn < p_dtClosingDate);


Avoiding non-SARGable OR, although I'm note sure this is much better...

 Where
    Case When p_strOperator = '=' THEN MyColumn ELSE p_dtClosingDate END = p_dtClosingDate
    AND
    Case When p_strOperator = '>' THEN MyColumn ELSE p_dtClosingDate+1 END > p_dtClosingDate
    AND
    Case When p_strOperator = '<' THEN MyColumn ELSE p_dtClosingDate-1 END < p_dtClosingDate
0

精彩评论

暂无评论...
验证码 换一张
取 消