22,209
社区成员
发帖
与我相关
我的任务
分享
/*
这里有一个表,我要实现的目的是,通过输入一个VID参数,如果这个参数为空就查询所有记录,如果不为空就查询,这个参数
的值(不能用动太语句)
*/
create table sel(
VID int,
Vname varchar,
)
insert into sel select 1,'aa'
union all select 2 , 'bb'
union all select 3 , 'cc'
select * from sel where @vid is null or vid=@vid
create table sel(
VID int,
Vname varchar(2),
)
insert into sel select 1,'aa'
union all select 2 , 'bb'
union all select 3 , 'cc'
declare @vid int
--set @vid=1
--1.(推荐)
if isnull(@vid,0)>0
select * from sel
else
select * from sel where vid=@vid
--2.(vid有非空约束时)
select * from sel where vid=isnull(@vid,vid)
drop table sel