22,208
社区成员
发帖
与我相关
我的任务
分享
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (name nvarchar(4),PrintNum int)
insert into [TB]
select N'张三',0 union all
select N'李四',1
declare @参数 int
select @参数 = 2
select * from tb
where PrintNum = case @参数 when 2 then PrintNum else @参数 end
----------------------
张三 0
李四 1
select * from tb
where PrintNum = case @参数 when 2 then PrintNum else @参数 end
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (name nvarchar(4),PrintNum int)
insert into [TB]
select N'张三',0 union all
select N'李四',1
--當@参数 為null時候查詢所有
declare @参数 int
select * from tb where printnum=isnull(@参数,printnum)
--0:表示要查询未打印的记录
set @参数=0
select * from tb where printnum=isnull(@参数,printnum)
--1:表示查询已打印的记录
set @参数=1
select * from tb where printnum=isnull(@参数,printnum)
/*
(2 個資料列受到影響)
name PrintNum
---- -----------
张三 0
李四 1
(2 個資料列受到影響)
name PrintNum
---- -----------
张三 0
(1 個資料列受到影響)
name PrintNum
---- -----------
李四 1
(1 個資料列受到影響)
*/
select
sum(case PrintNum when 0 then 1 else 0 end) as 未打印的数据,
sum(case when PrintNum>0 then 1 else 0 end) as 已打印数据,
count(1) as 全部数据
from
tb
where
name='参数'