CREATE PROCEDURE P
(
@Type int,
@TypeID int
)
as
begin
if @Type=1
select * from table1 t
where @TypeID=t.Aid
if @Type=2
select * from table1 t
where @TypeID=t.Bid
if @Type<>1 and @Type<>2
select * from table1 t
where @TypeID=t.Cid
end
(
@Type int,
@TypeID int
)
as
begin
if @Type=1
select * from table1 t
where @TypeID=t.Aid
if @Type=2
select * from table1 t
where @TypeID=t.Bid
if @Type<>1 and @Type<>2
select * from table1 t
where @TypeID=t.Cid
end
一个例create table t
(col int,col1 int,col2 varchar(20))insert t
select 1,2,'22' union all
select 1,3,'23' union all
select 1,4,'24' union all
select 2,5,'34' union all
select 3,6,'45'
go
declare @t1 int, @t2 varchar(20)
select @t1=2,@t2='23'
select * from t
where
@t1=(case @t2
when '22' then t.col
when '23' then t.col1
else t.col2
end
)
drop table t
col col1 col2
----------- ----------- --------------------
1 2 22(所影响的行数为 1 行)