Tuesday, February 14, 2012

determining the where statement

Hello,

Does anyone know how I can write the where statement according to the value?
In other words,
a command name would determine the where statement but the select statement is always the same.

This does not work:


create procedure new_procedure
(@.cid int, @.command VarChar(25))
Select * from a table
If @.command = 'pick1' begin
Where this = @.cid and that = 'Yes'
End
Else If @.command = 'pick2' begin
Where this = @.cid and that = 'No'
End
Else If @.command = 'pick3' begin
Where this = @.cid and that = 'No' and datefield >= DateAdd(d, -5, getdate())
End

something like that there.
Any ideas?
Thanks
Ehow many distinct values for @.command do you see ? is that something fixed like 3-4 options or would you have plenty of them..if you have only 3-4 options like you have above then you can do something like:


create procedure new_procedure
(@.cid int, @.command VarChar(25)) AS

begin

If @.command = 'pick1'
select * from table Where this = @.cid and that = 'Yes'
Else If @.command = 'pick2'
select * from table Where this = @.cid and that = 'No'
Else If @.command = 'pick3'
select * from table Where this = @.cid and that = 'No' and datefield >= DateAdd(d, -5,
getdate())

end


HTH

No comments:

Post a Comment