0

I have a stored procedure that is returning me some records. Now I want to change the condition on which the records are being shown on the basis of a condition

select * from table
WHERE cond1 = c1
AND cond2 = c2

basically I want a structure like this

if(Val1= Val2) then  
select * from table
WHERE cond1 = c1
AND cond2 = c2
AND cond3 = c3
else
select * from table
WHERE cond1 = c1
AND cond2 = c2

Since the stored proc is huge and I can't change too much in that so I can only change the where condition using if statement.

4

2 に答える 2

3

where句に条件を含めるだけです。

select * from table
WHERE cond1 = c1
AND cond2 = c2
AND (Val1 <> Val2 OR cond3 = c3)

この構造が拡大し続ける場合(おそらく、さまざまな可能なパラメーターを使用して何らかの形式の検索を実行している場合)、T-SQLでのErlandSommarskogの動的検索条件を読む価値があります。

于 2012-12-18T07:29:01.947 に答える
0

Try this:

IF Val1 = Val2 
BEGIN

END
ELSE
BEGIN

END
于 2012-12-18T07:15:30.113 に答える