MSSQL 中没有CASE语句,你可以用 IF {表达式} BEGIN --你的语句 END Else Being --你的语句 end
case when then else case when then else ... end 有是有這種結構,但他隻是針對一個值 例如 case a when 0 then 1 case a when 2 then 3 end 當a=0是,a會變為1 當a=2
create proc p_choose @input char(1) as declare @str_sql varchar(8000) set @str_sql=(case @input when '0' then 'tab1' when '1' then 'tab2' end) set @str_sql='select * from '+@str_sql+'' exec(@str_sql) end
IF {表达式}
BEGIN
--你的语句
END
Else
Being
--你的语句
end
case when then else
...
end
有是有這種結構,但他隻是針對一個值
例如 case a when 0 then 1
case a when 2 then 3 end
當a=0是,a會變為1
當a=2
@input char(1)
as
declare @str_sql varchar(8000)
set @str_sql=(case @input
when '0' then 'tab1'
when '1' then 'tab2' end)
set @str_sql='select * from '+@str_sql+''
exec(@str_sql)
end