Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8362877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:02:40+00:00 2026-06-09T12:02:40+00:00

OK. I got a lot of help here earlier working with a SQL backend

  • 0

OK. I got a lot of help here earlier working with a SQL backend to a simple … just not for me 🙁 … time clock solution for the small office I work in, so I’m back for more!

My table I’m currently working with consists of 6 columns:

  1. clockDate date not null PK
  2. userName varchar(50) not null PK
  3. clockIn time(0)
  4. breakOut time(0)
  5. breakIn time(0)
  6. clockOut time(0)

I though I had figured out my IF NOT EXISTS INSERT ELSE UPDATE statement from my last question, but now I’m trying to use it in a Stored Procedure, rather than a plain query window, with no success.

Basically a user clocking in is a no-brainer. However, if the user doesn’t clock in, but they clock out for lunch, the statement needs to create the row instead of updating an existing row. Ok so here’s my stored procedure:

ALTER PROCEDURE dbo.BreakOut
(
    @userName varchar(50)
)
AS

IF EXISTS (SELECT * FROM Clock WHERE clockDate = GETDATE() AND userName = @userName)
    BEGIN
        UPDATE Clock SET breakOut = GETDATE() 
            WHERE clockDate = GETDATE() AND userName = @userName
    END
ELSE
    BEGIN
        INSERT INTO Clock (clockDate, userName, breakOut) 
            VALUES (GETDATE(), @userName, GETDATE())
    END

Here’s my problem… If the user DID clock in for the day I get a primary key violation because the stored procedure is still trying to run the INSERT part of the statement and never runs the UPDATE line. I’ve tried it flipped with an IF NOT EXISTS as well with the same result. What’s the trick to get IF-ELSE to work in a stored procedure? Can this be done they way I’m thinking or do I have to study Merge statement? My plan is to run the stored procedures from a simple Visual Basic program on each workstation. Maybe I’m getting in over my head 🙁 To bad my boss is too cheap to just buy a time clock solution!

EDIT:

Thank you ALL for your help!! I’m falling in love with this site, questions get answers SO FAST!!! Here is my working stored procedure:

ALTER PROCEDURE dbo.BreakOut
(
    @userName varchar(50)
)
AS

IF EXISTS (SELECT * FROM Clock WHERE DateDiff(dd, GetDate(),clockDate) = 0 AND userName = @userName)
    BEGIN
        UPDATE Clock SET breakOut = GETDATE() 
            WHERE DateDiff(dd, GetDate(),clockDate) = 0 AND userName = @userName
    END
ELSE
    BEGIN
        INSERT INTO Clock (clockDate, userName, breakOut) 
            VALUES (GETDATE(), @userName, GETDATE())
    END

Is this proper, or could it be improved more? Again Thank You ALL SO MUCH!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:02:42+00:00Added an answer on June 9, 2026 at 12:02 pm

    This is probably the problem right here: WHERE clockDate = GETDATE()

    GetDate returns the current date AND the current time, which wouldn’t match up with clockDate. You can compare the dates with DateDiff instead:

    WHERE DateDiff(dd, GetDate(),clockDate) = 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to parse RSS chaanal with simple-rss lib. Unfortunately I got a lot
I'm new in ASP.NET and already got a lot of answers here searching on
I've got a lot controller in my Codeigniter apps, ex: Signup, Profile, Main, etc..
My web application has got a lot of service tables/entities, such as payment_methods ,
I was installing a travian script on localhost xammp, and i got a lot
I'm trying to re-learn systems analysis. I've got a lot of object-oriented thinking for
I have got map a lot of ranges to a value like 0-300 =
Hey guys I've got a whole lot of HTML to format with myString +
(Edited a lot) I've got some classes with Abstracts Members. The concrete type of
I got a website which contains a lot of projects with each project containing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.