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. sql server insert into select + in select part if else

sql server insert into select + in select part if else

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelptutorial
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.
  • K Offline
    K Offline
    Karan_TN
    wrote on last edited by
    #1

    How to execute the below piece of code correctly. Please help me.

    INSERT INTO #TempTable
    (PatchAppId, PatchName,HostName)

            if (@Id = 0)
            BEGIN
            SELECT PatchAppId, PatchName,HostName FROM Vw\_ProductionAutoMailData1
            where Criticality=@Criticality
            end
            else
            begin
            SELECT PatchAppId, PatchName,HostName FROM Vw\_ProductionAutoMailData2
            where ServerTypeName=@ServerTypeName
            end
    
    M 1 Reply Last reply
    0
    • K Karan_TN

      How to execute the below piece of code correctly. Please help me.

      INSERT INTO #TempTable
      (PatchAppId, PatchName,HostName)

              if (@Id = 0)
              BEGIN
              SELECT PatchAppId, PatchName,HostName FROM Vw\_ProductionAutoMailData1
              where Criticality=@Criticality
              end
              else
              begin
              SELECT PatchAppId, PatchName,HostName FROM Vw\_ProductionAutoMailData2
              where ServerTypeName=@ServerTypeName
              end
      
      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Erk! You have an insert statement Then an if statement on a variable that is not in scope (of the question) Try moving the insert inside the if statement, so you will need the insert twice if begin insert select end else begin insert select end

      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