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. Insert/Update command with Case in sql server

Insert/Update command with Case in sql server

Scheduled Pinned Locked Moved Database
databasesql-serversysadmintutorialquestion
2 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.
  • U Offline
    U Offline
    Uma Kameswari
    wrote on last edited by
    #1

    I have to write a insert/update command with in a case. IS it possible? for example select case(select count(*) from table1 a where a.no=b.no and a.act=b.act) when 0 then insert into temp(name,desc,status) values(b.name,b.desc,1) when 1 then update temp set name=b.name,desc = b.desc,status=2 where id=b.id else insert into temp(name,desc,status) values(b.name,b.desc,3) end from table2 b But as case statement accepts only expressions can some one let me know how the above can be modified

    W 1 Reply Last reply
    0
    • U Uma Kameswari

      I have to write a insert/update command with in a case. IS it possible? for example select case(select count(*) from table1 a where a.no=b.no and a.act=b.act) when 0 then insert into temp(name,desc,status) values(b.name,b.desc,1) when 1 then update temp set name=b.name,desc = b.desc,status=2 where id=b.id else insert into temp(name,desc,status) values(b.name,b.desc,3) end from table2 b But as case statement accepts only expressions can some one let me know how the above can be modified

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      You cannot combine insert and update statements, so you have to execute two separate commands. However you can execute them in a single batch (or write a procedure to wrap them).

      The need to optimize rises from a bad design.My articles[^]

      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