Setting Value depending on condition

  • My Stored Procedure Gets a value from front end.i,e. L0, L1,L2,L3

    Depening on what value is send from the front end, i need to set the value for a variable. Can this be done using CASE statement?? I have used the IF-ELSE statement to perform this;

    CREATE PROCEDURE usp_MyProcedure

    (

    @Type NVARHCHAR(2)

    )

    AS

    BEGIN

    DECLARE @Var INT

    IF(@Type ='L0') @Var=0

    ELSE IF (@Type ='L1') @Var=1

    ELSE IF (@Type ='L2') @Var=2

    ELSE IF (@Type ='L3') @Var=3

    Can the same be achieve by CASE statement? Or the above holds good

  • naveenreddy.84 (6/22/2009)


    My Stored Procedure Gets a value from front end.i,e. L0, L1,L2,L3

    Depening on what value is send from the front end, i need to set the value for a variable. Can this be done using CASE statement?? I have used the IF-ELSE statement to perform this;

    CREATE PROCEDURE usp_MyProcedure

    (

    @Type NVARHCHAR(2)

    )

    AS

    BEGIN

    DECLARE @Var INT

    IF(@Type ='L0') @Var=0

    ELSE IF (@Type ='L1') @Var=1

    ELSE IF (@Type ='L2') @Var=2

    ELSE IF (@Type ='L3') @Var=3

    Can the same be achieve by CASE statement? Or the above holds good

    I've got two different ways to handle what you're doing:

    set @Var = case @Type

    when 'L0' then 0

    when 'L1' then 1

    when 'L2' then 2

    when 'L3' then 3

    end

    and:

    set @Var = convert(int, substring(@type, 2, len(@Type)))

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • Thanks a lot!!!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply