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. using ?: operation in sql server 2008

using ?: operation in sql server 2008

Scheduled Pinned Locked Moved Database
helpquestiondatabasesql-serversysadmin
5 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.
  • R Offline
    R Offline
    reza assar
    wrote on last edited by
    #1

    hi all i want to use some thing like that

    set @a=@AnimalName == "Elephant"? "savannah": "unknown"

    but it has an error in == how can i fix it? besides i want to use conditional ?: operation to concat some strings like bellow how i should so;ve this problem?

    N'insert into TBL Values(@Val1,'+ @Condition = 1? N'val2':N'val3')+',...'

    D 1 Reply Last reply
    0
    • R reza assar

      hi all i want to use some thing like that

      set @a=@AnimalName == "Elephant"? "savannah": "unknown"

      but it has an error in == how can i fix it? besides i want to use conditional ?: operation to concat some strings like bellow how i should so;ve this problem?

      N'insert into TBL Values(@Val1,'+ @Condition = 1? N'val2':N'val3')+',...'

      D Offline
      D Offline
      dasblinkenlight
      wrote on last edited by
      #2

      Conditionals in Transact-SQL are expressed using the CASE statement[^]. Examples in the documentation should help you construct what you need.

      R 1 Reply Last reply
      0
      • D dasblinkenlight

        Conditionals in Transact-SQL are expressed using the CASE statement[^]. Examples in the documentation should help you construct what you need.

        R Offline
        R Offline
        reza assar
        wrote on last edited by
        #3

        dear friend 1- thanks to your reply it works for part of my storedprocedure 2- i heard that CASE is not high performance statement 3- what should i do if i want to shorten this block of code?

        if(@flag=1) then
        select * from TBL1
        ELSE
        select * from TBL2

        i think this is not applicable to using CASE am i right?

        D 1 Reply Last reply
        0
        • R reza assar

          dear friend 1- thanks to your reply it works for part of my storedprocedure 2- i heard that CASE is not high performance statement 3- what should i do if i want to shorten this block of code?

          if(@flag=1) then
          select * from TBL1
          ELSE
          select * from TBL2

          i think this is not applicable to using CASE am i right?

          D Offline
          D Offline
          dasblinkenlight
          wrote on last edited by
          #4

          1. No problem 2. This does not make any more sense than "for loops are not high performance statements". 3. Assuming TBL1 and TBL2 have identical structure, you can do it like this:

          select * from TBL1 where @flag=1
          union all
          select * from TBL2 where @flag<>1

          R 1 Reply Last reply
          0
          • D dasblinkenlight

            1. No problem 2. This does not make any more sense than "for loops are not high performance statements". 3. Assuming TBL1 and TBL2 have identical structure, you can do it like this:

            select * from TBL1 where @flag=1
            union all
            select * from TBL2 where @flag<>1

            R Offline
            R Offline
            reza assar
            wrote on last edited by
            #5

            very nice thanks

            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