如果a = 4
想从testTable
获得所有记录(6行)。 4
是SP中的默认参数。案例表达式在where子句中
create table testTable(a int, b int, c int)
go
insert into testTable values(2, 101, 100000)
go
insert into testTable values(2, 101, 100001)
go
insert into testTable values(3, 101, 100002)
go
insert into testTable values(3, 102, 100003)
go
insert into testTable values(4, 1, 100004)
go
insert into testTable values(4, 1, 100005)
go
create proc SPtest
@a int = 4,
@b int = 1
as
select * from testTable where a = @a and b = @b
exec SPtest 2, 101
以上工作正常。但我需要这样的东西:
declare @a int
set @a = 4
select *
from testTable
where a = case @a when 4 then select distinct a from testTable end
'CASE'在SQL Server只能返回 “原子” 的价值观 - 你不能运行'CASE'语句中的T-SQL代码。你需要从表中读取这些不同的值并将它们存储在一个变量**之前**执行此选择与案例在where子句 –