日期:2014-05-18  浏览次数:20463 次

我要实现一个查询,只用查询语句,大家帮我看看,很久没有弄数据库了有点忘本了。
我现在有一张表比如     student   ,里面有两个字段   ID,name.
我实现的功能也是很简单的。
select   *   from   student   where   'name '   =?
?是要传进去的值。
我要实现的是:如果?是null,我要显示所有的记录,如果?不是null,我要显示查询的结果,大家帮我想想怎么来组织这个查询语句(不用存储过程,就用一句查询语句)

------解决方案--------------------
select * from student where name=isnull(@name,name)


--------------
好方法
------解决方案--------------------

declare @s int
set @s=null

select * from jobs where job_id=isnull(@s,job_id)

job_id job_desc min_lvl max_lvl
------ -------------------------------------------------- ------- -------
1 New Hire - Job not specified 10 10
2 Chief Executive Officer 200 250
3 Business Operations Manager 175 225
4 Chief Financial Officier 175 250
5 Publisher 150 250
6 Managing Editor 140 225
7 Marketing Manager 120 200
8 Public Relations Manager 100 175
9 Acquisitions Manager 75 175
10 Productions Manager 75 165
11 Operations Manager 75 150
12 Editor 25 100
13 Sales Representative 25 100
14 Designer 25 100

(14 row(s) affected)