SQL Server多條件查詢的實現
SQL Server多條件查詢我們經常會用到,下面就教您如何使用存儲過程實現SQL Server多條件查詢,希望對您學習SQL Server多條件查詢方面有所幫助。
/*查詢資訊類別列表*/
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'get_list_newscate' AND type = 'P')
DROP PROCEDURE get_list_newscate
GO
create proc get_list_newscate
@newscate_pid int=null,
@newscate_depth int=null,
@newscate_language int=null
as
select * from [news category] where 1=1
and (@newscate_pid is null or newscate_pid=@newscate_pid)
and (@newscate_depth is null or newscate_depth=@newscate_depth)
and (@newscate_language is null or newscate_language=@newscate_language)
order by newscate_orderid
此存儲過程可以實現SQL Server多條件查詢。
可以用于網站高級檢索的功能
查詢條件默認為null
程序中值類型不能為null可以在類型后加一個?這樣就可以null
比如:int i=null 錯誤 int? i=null正確
where 1=1是為了當查詢條件都不需要用到時就查詢全部數據
【編輯推薦】