[dba-SQLServer] Is there a better way to do this?

Mark Breen marklbreen at gmail.com
Tue Jan 25 02:53:38 CST 2011


Hello David,

two quick suggestions

1) read up on case statements and practice them a little, they might clean
up the code

2) sometimes you can improve readability by creating a few neat userdefinted
functions, they are as easy to create as a sproc and one written you could
maybe restructure your code to read like


if ufn_BasicChange()
Begin
--do stuff
End

if ufn_OtherTypeOfChange()
Begin
--do stuff
End


or alternately, you could combine both the case and the values you are
playing with

Case ufn_AllPossibileOptions(@NewDate)

  When 'Basic' then begin ............. end
  When 'Has No Date' then begin ............. end
  When 'Has A Date' then begin ............. end
  Else  begin ............. end

End


Hope that is useful, John Colby thought me about how to use functions in
1998 so credit goes to him :)

Mark


On 21 January 2011 19:59, David McAfee <davidmcafee at gmail.com> wrote:

> Is there a better way to do this?
>
> IF (@ChangeKey IN(0,1,2,3,4,5,8,9,10,11,12,13))
>
>    BEGIN
>       --Do a bunch of stuff here
>    END
> ELSE IF (@ChangeKey IN(6,7,14,15) AND @NewStart >@OldEnd)
>       BEGIN
>       --Do a bunch of stuff here
>    END
> ELSE IF (@ChangeKey IN(6,7,14,15) AND @NewEnd <@OldStart)
>       BEGIN
>       --Do a bunch of stuff here
>    END
> ELSE IF (@ChangeKey IN(6,7,14,15) AND @NewStart <@OldStart AND @NewEnd
> >@OldEnd)
>       BEGIN
>       --Do a bunch of stuff here
>    END
> _______________________________________________
> dba-SQLServer mailing list
> dba-SQLServer at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/dba-sqlserver
> http://www.databaseadvisors.com
>
>



More information about the dba-SQLServer mailing list