sql不用拼接语句实现动态查询条件

DECLARE @oFrom INT
SET @oFrom = 2

SELECT * FROM baseinfo 
WHERE 1 = 1
AND (
    (@oFrom = 0 and Type = 'OrderFrom' ) or ( @oFrom = 1 and Type = 'BillStatus' ) or ( @oFrom = 2 and Type = 'Breakfast')
)

or的条件可自由添加,尤其适用互斥条件的查询。

posted @ 2017-05-27 10:11  好人卡收藏家  阅读(3007)  评论(0编辑  收藏  举报