Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. End Case in a Variable

End Case in a Variable

Scheduled Pinned Locked Moved Database
databasehelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    berba
    wrote on last edited by
    #1

    I have a case statement. I want to save the case value in a variable so that I can use the variable later in the stored procedure. Ex: case when Flag = 'Y' then CONVERT(varchar(10), (LastDate), 120) end as @var1, case when @var1 = ... then ..... end Please help

    M 1 Reply Last reply
    0
    • B berba

      I have a case statement. I want to save the case value in a variable so that I can use the variable later in the stored procedure. Ex: case when Flag = 'Y' then CONVERT(varchar(10), (LastDate), 120) end as @var1, case when @var1 = ... then ..... end Please help

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      SET @VariableName = CASE Flag When 'Y' then etc...

      or

      select @VariableName = CASE Flag When 'Y' then etc...

      Never underestimate the power of human stupidity RAH

      B 1 Reply Last reply
      0
      • M Mycroft Holmes

        SET @VariableName = CASE Flag When 'Y' then etc...

        or

        select @VariableName = CASE Flag When 'Y' then etc...

        Never underestimate the power of human stupidity RAH

        B Offline
        B Offline
        berba
        wrote on last edited by
        #3

        the Stored proc is as follows: select empno, datejoined, case when Flag = 'Y' then CONVERT(varchar(10), (LastDate), 120) end as @var1, case when @var1 = ... then ..... end from tableemp

        M 1 Reply Last reply
        0
        • B berba

          the Stored proc is as follows: select empno, datejoined, case when Flag = 'Y' then CONVERT(varchar(10), (LastDate), 120) end as @var1, case when @var1 = ... then ..... end from tableemp

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          You are combining a select query and an assignment query, not allowed! You need to nest your case statements as you need to test the case for each record, variable assignment is a once only operation (using a cursor spit would allow you to do the record by record processing). This example test the value of flag but note that it must know what to expect in the nested case, your ... cannot be a random date, it must be a testable value!

          SELECT Records,
          CASE Flag WHEN 'FX' THEN CASE Records WHEN 0 THEN 'Failed' ELSE 'Ok' END
          WHEN 'MM' THEN CASE Records WHEN 0 THEN 'Failed' ELSE 'Ok' END
          WHEN 'TZ' THEN CASE Records WHEN 0 THEN 'Failed' ELSE 'Ok' END END
          FROM ProcessLog PL

          Never underestimate the power of human stupidity RAH

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups