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. How to update a table using a Select statement in the Where Clause

How to update a table using a Select statement in the Where Clause

Scheduled Pinned Locked Moved Database
toolstutorialquestionannouncement
3 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.
  • S Offline
    S Offline
    Skanless
    wrote on last edited by
    #1

    I am trying to update a record in an Employee work hours table to record the employee time out. I need to find the last time the employee clocked in then assosciate the time out with the record. thus having a time in and timeout data. UPDATE dbo.EmpWorkHours SET Emp_Timeout = getdate() where Emp_Timeout in (SELECT TOP 1 Emp_timeout FROM dbo.EmpWorkHours WHERE FK_Emp_Login_ID = 'MDoe74' and Emp_Timeout IS NULL ORDER BY RecordNum DESC ) The script looks like it should be doing what I want it to do but it never finds any record, thought the record exist. Any assistance will be great appreciated.

    Skan If you knew it would not compile why didn't you tell me?!?!?!

    J S 2 Replies Last reply
    0
    • S Skanless

      I am trying to update a record in an Employee work hours table to record the employee time out. I need to find the last time the employee clocked in then assosciate the time out with the record. thus having a time in and timeout data. UPDATE dbo.EmpWorkHours SET Emp_Timeout = getdate() where Emp_Timeout in (SELECT TOP 1 Emp_timeout FROM dbo.EmpWorkHours WHERE FK_Emp_Login_ID = 'MDoe74' and Emp_Timeout IS NULL ORDER BY RecordNum DESC ) The script looks like it should be doing what I want it to do but it never finds any record, thought the record exist. Any assistance will be great appreciated.

      Skan If you knew it would not compile why didn't you tell me?!?!?!

      J Offline
      J Offline
      joemonvarghese
      wrote on last edited by
      #2

      hi Check whether the field Emp_Timeout is null or ' '(Space) in the table dbo.EmpWorkHours Regards Joe -- modified at 1:50 Friday 12th October, 2007

      1 Reply Last reply
      0
      • S Skanless

        I am trying to update a record in an Employee work hours table to record the employee time out. I need to find the last time the employee clocked in then assosciate the time out with the record. thus having a time in and timeout data. UPDATE dbo.EmpWorkHours SET Emp_Timeout = getdate() where Emp_Timeout in (SELECT TOP 1 Emp_timeout FROM dbo.EmpWorkHours WHERE FK_Emp_Login_ID = 'MDoe74' and Emp_Timeout IS NULL ORDER BY RecordNum DESC ) The script looks like it should be doing what I want it to do but it never finds any record, thought the record exist. Any assistance will be great appreciated.

        Skan If you knew it would not compile why didn't you tell me?!?!?!

        S Offline
        S Offline
        Skanless
        wrote on last edited by
        #3

        Thanks for your response. I figured it out, using the code below. UPDATE dbo.EmpWorkHours SET Emp_Timeout = getdate() where RecordNum = (SELECT TOP 1 RecordNum FROM dbo.EmpWorkHours WHERE FK_Emp_Login_ID = 'MDoe74' and Emp_Timeout IS NULL ORDER BY RecordNum DESC ) What was wrong with it you may ask? Well, I was selecting the Emp_timeout field which was null. I was then telling to update a record where getdate() equals the value in the Emp_Timeout field. That would never happen because at the time of the query Emp_timeout field is NULL thus it will never equal getdate(). Hope this helps.

        Skan If you knew it would not compile why didn't you tell me?!?!?!

        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