在sqlserver 中用一条语句实现 如果为空显示全部数据

本文介绍了一段SQL脚本,用于在临时数据库中创建测试数据表T_20081010,并插入了多条记录示例。此外,还提供了一个查询示例,演示如何使用CASE WHEN语句来过滤数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

--生成测试数据
if not object_id('Tempdb..T_20081010') is null
    drop table T_20081010
Go
Create table T_20081010([ID] int,[S_ID] nvarchar(1),[P_ID] nvarchar(10))
Insert T_20081010
select 1,'a','2009-02-00' union all
select 2,'a','2009-02-28' union all
select 3,'b','2009-01-28' union all
select 4,'b','2009-01-25' union all
select 5,'b','2009-02-27' union all
select 6,'b','2009-02-26'
Go
--Select * from T_20081010 
--实现:
declare @P int 
set @P=''
select * from T_20081010
where (case  when (id=@p) then  1  when @p='' then 1  else 0 end)=1 
<style type="text/css">.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } </style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值