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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Checking if a field exists in a table before adding it to another table

Checking if a field exists in a table before adding it to another table

Scheduled Pinned Locked Moved Database
helpquestion
4 Posts 4 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.
  • T Offline
    T Offline
    Twyce
    wrote on last edited by
    #1

    Hi Guys If anyone can help me with this it would be greatly appreciated. K here is what's happening.I am supposed to check if a student exists in the STUDENT table and then check if a module exists in the MODULE table.if they exist in both tables I must add the student(StudentNumber) and the module(ModuleCode) to the STUDENTMODULE table(register the student). How do i do this plz help

    D B P 3 Replies Last reply
    0
    • T Twyce

      Hi Guys If anyone can help me with this it would be greatly appreciated. K here is what's happening.I am supposed to check if a student exists in the STUDENT table and then check if a module exists in the MODULE table.if they exist in both tables I must add the student(StudentNumber) and the module(ModuleCode) to the STUDENTMODULE table(register the student). How do i do this plz help

      D Offline
      D Offline
      David Mujica
      wrote on last edited by
      #2

      To find if a record exists, you would use a select statement ... select count(*) from student where studentNumber = [myStud] if count = 0 then insert StudentNumber,ModuleCode into STUDENTModule values (myStud,myMoodule) This is not actual code, but pseudocode meant to point you in the right direction. I won't do your homework for you. :)

      1 Reply Last reply
      0
      • T Twyce

        Hi Guys If anyone can help me with this it would be greatly appreciated. K here is what's happening.I am supposed to check if a student exists in the STUDENT table and then check if a module exists in the MODULE table.if they exist in both tables I must add the student(StudentNumber) and the module(ModuleCode) to the STUDENTMODULE table(register the student). How do i do this plz help

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

        if(select count(*) from students where studentnamecolumn = 'studentname') = 0 begin insert into students values (value1,value1......) end


        I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

        1 Reply Last reply
        0
        • T Twyce

          Hi Guys If anyone can help me with this it would be greatly appreciated. K here is what's happening.I am supposed to check if a student exists in the STUDENT table and then check if a module exists in the MODULE table.if they exist in both tables I must add the student(StudentNumber) and the module(ModuleCode) to the STUDENTMODULE table(register the student). How do i do this plz help

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          You can do this with an insert into table, e.g.

          insert into mytable (value1, value2, value3)
          select @value1, @value2, @value3
          where @myitem not in (select myitem from table2)

          Deja View - the feeling that you've seen this post before.

          My blog | 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