0

Suggest me the proper syntex for below raw condition.

ALTER PROCEDURE [dbo].[pro_name]
@number as int ,

Select * from table 
if(@number=0)
begin
set @number=select max(number)from table
end
where table.number=@number


here i need to set @number with max value if input value passed is '0'. and also want to use th e same in wherer clause.

3
  • the WHERE clause - do you want @number to be used before or after calculating MAX ? Commented Mar 9, 2012 at 5:05
  • where table.number= case number when 0 then MAX(table.number) else number (NOT WORKING ) Commented Mar 9, 2012 at 5:17
  • @YS. i want where clause with conditional value i.e if else before where clause. in any case use number but with diff value (according to the condition) Commented Mar 9, 2012 at 5:19

2 Answers 2

2
ALTER PROCEDURE [dbo].[pro_name]
@number as int=0 --I recommend a default value here too
as
set @number=case @number when 0 then (select max(number)from [table]) else @number end;

Select * from [table] t
where t.number=@number;
go
Sign up to request clarification or add additional context in comments.

Comments

0

EDIT:

Since there is not point in having two exactly the same answers and I was beaten, here is an alternate solution:

alter procedure [dbo].[pro_name]
@number as int as

select top 1 * from [table]
where number = @number or @number = 0
order by [table].number desc

However this will only work if the number column is unique, which may or may not be the case in your case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.