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是为了当查询条件都不需要用到时就查询全部数据

 

 

【编辑推荐】

SQL Server跨服务器查询

SQL Server资源锁模式大全

sql server查询字段详细信息

sql server查询平均值的实现

SQL Server FROM子句的语法

THE END