2017-08-01 89 views
2

我有以下存儲過程:CASE或IF然後選擇SQL

ALTER Procedure _MySP (@oDate date) 
AS 
BEGIN 
    SELECT * 
    FROM _table1 
    WHERE oDate >= @oDate 
END 

我想修改查詢的東西,如:

IF @oDate <= '2017-06-01' 
THEN 
    SELECT * 
    FROM _table2 
ELSE 
    SELECT * 
    FROM _table1 
END 

我怎樣才能做到這一點?請指教。

謝謝。

回答

3

你試過這個嗎?

alter Procedure _MySP (@oDate date) 
as 
begin 
    If @oDate <= '2017-06-01' 
     SELECT * FROM _table2 
    else 
     SELECT * FROM _table1 
end; 
0
alter Procedure _MySP (@oDate date) 
as 
begin 
    select * from _table1 where @oDate => '2017-06-01' 
    select * from _table2 where @oDate <= '2017-06-01' 
end 

應該做的伎倆。希望你沒有平等的案例思想。

0
IF @oDate <= '2017-06-01' 
BEGIN 
    SELECT * FROM _table1 
    --where-- 
END 
ELSE 
BEGIN 
    SELECT * FROM _table2 
    --where-- 
END 
0

請試試這個。

ALTER PROCEDURE _MySP (@oDate date) 
BEGIN 
IF (@oDate < = '2017-06-01') 

BEGIN 

    SELECT * FROM _table2 

END 

ELSE 

BEGIN 

    SELECT * FROM _table1 

END 


END