If statments in MSSQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Normann
    New Member
    • Jan 2007
    • 17

    If statments in MSSQL

    I’m am trying to create a StoredProc that will, if a value is below a limit it will insert one value or if the value is above a limit then it will insert another value. Sort of like this in C programming:

    Code:
                if (ValueOriginal >= 3)
                {
                    if (ValueOriginal <= 10)
                        ValueToInsert = ValueOriginal;
                    else
                        ValueToInsert = 0;
                }
                else
                    ValueToInsert = 0;
    Hope Someone can help me

    NormanTheDane
  • prileep
    New Member
    • Jan 2007
    • 20

    #2
    Originally posted by Normann
    I’m am trying to create a StoredProc that will, if a value is below a limit it will insert one value or if the value is above a limit then it will insert another value. Sort of like this in C programming:

    Code:
                if (ValueOriginal >= 3)
                {
                    if (ValueOriginal <= 10)
                        ValueToInsert = ValueOriginal;
                    else
                        ValueToInsert = 0;
                }
                else
                    ValueToInsert = 0;
    Hope Someone can help me

    NormanTheDane
    If u just want to know how to use IF state ment in SQL Server,

    Format one:
    IF <condition> <then code to be executed when condition true>
    Format two:
    IF <condition>
    BEGIN
    <then code to be executed when condition true>
    END
    ELSE
    BEGIN
    < else code to be executed when condition is false>
    END

    Comment

    • almaz
      Recognized Expert New Member
      • Dec 2006
      • 168

      #3
      Code:
      insert <<YourTableName>> (<<ListOfFields>>) values 
      (case 
          when @ValueOriginal >= 3 and @ValueOriginal <= 10 then @ValueOriginal 
          else 0 
      end, <<OtherFields>>)

      Comment

      • hariharanmca
        Top Contributor
        • Dec 2006
        • 1977

        #4
        Originally posted by Normann
        I’m am trying to create a StoredProc that will, if a value is below a limit it will insert one value or if the value is above a limit then it will insert another value. Sort of like this in C programming:

        Code:
                    if (ValueOriginal >= 3)
                    {
                        if (ValueOriginal <= 10)
                            ValueToInsert = ValueOriginal;
                        else
                            ValueToInsert = 0;
                    }
                    else
                        ValueToInsert = 0;
        Hope Someone can help me

        NormanTheDane


        Code:
          if  ValueOriginal >= 3
                    Begin
                        if  ValueOriginal <= 10 
                            ValueToInsert = ValueOriginal
                        else
                            ValueToInsert = 0
                    end
                    else
                        ValueToInsert = 0

        Try This

        Comment

        • Normann
          New Member
          • Jan 2007
          • 17

          #5
          Thanks for all your help, I have it working now.

          Comment

          Working...