1.聲明一個sql變量,declare @sql1 nvarchar(2000) 或其餘方法聲明。經過IF 條件 begin .......;end ELSEIF 條件 begin .......;end ELSE begin .......;end 進行sql字符傳的拼接。最後再執行 EXEC sp_executesql @sql1 來執行sql語句。java
2.若是判斷條件簡單的話能夠直接在查詢條件中進行判斷,其就是經過OR,AND運算符來語義話語句sql
select * from user cc where and cc.userid =v_userid and ((v_limit is null) or ( ((v_numbera is null) or instr(substr(card_no,-v_limit),v_numbera) = 0) and( (v_numberb is null) or instr(substr(card_no,-v_limit),v_numberb) = 0) ) )