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

  • SEARCH
  • Home
  • 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 4620050
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:30:28+00:00 2026-05-22T02:30:28+00:00

I have a table containing a student-grade relationship: Student Grade StartDate EndDate 1 1

  • 0

I have a table containing a student-grade relationship:

Student   Grade   StartDate   EndDate
   1        1    09/01/2009    NULL
   2        2    09/01/2010    NULL
   2        1    09/01/2009   06/15/2010

I am trying to write a stored procedure that takes Student, Grade, and StartDate, and I would like it to

  1. check to make sure these values are not duplicates
  2. insert the record if it’s not a duplicate
  3. if there is an existing student record, and it has a EndDate = NULL, then update that record with the StartDate of the new record.

For instance, if I call the procedure and pass in 1, 2, 09/01/2010, I’d like to end up with:

Student   Grade   StartDate   EndDate
   1        2    09/01/2010    NULL
   1        1    09/01/2009   09/01/2010
   2        2    09/01/2010    NULL
   2        1    09/01/2009   06/15/2010

This sounds like I could use MERGE, except that I am passing literal values, and I need to perform more than one action. I also have a wicked headache this morning and can’t seem to think clearly, so I am fixating on this MERGE solution. If there is a more more obvious way to do this, don’t be afraid to point it out.

  • 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-05-22T02:30:28+00:00Added an answer on May 22, 2026 at 2:30 am

    You can use a MERGE even if you are passing literal values. Here’s an example for your issue:

    CREATE PROCEDURE InsertStudentGrade(@Student INT, @Grade INT, @StartDate DATE)
    AS
    BEGIN;
    
        MERGE StudentGrade AS tbl
         USING (SELECT @Student AS Student, @Grade AS Grade, @StartDate AS StartDate) AS row
         ON tbl.Student = Row.Student AND tbl.Grade = row.Grade
        WHEN NOT MATCHED THEN
          INSERT(Student, Grade, StartDate)
           VALUES(row.Student, row.Grade, row.StartDate)
        WHEN MATCHED AND tbl.EndDate IS NULL AND tbl.StartDate != row.StartDate THEN
          UPDATE SET
            tbl.StartDate = row.StartDate;
    
    END;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table containing user-account permissions and I'm trying to write a query
I have a table containing hundreds of entries and I am trying to delete
I have a table containing view/click records. The time is stored in a unix
I have a simple table containing Student Numbers and corresponding Teacher Numbers, and I
I have a table containing hundreds of columns many of which are null, and
I have a table containing latitude and longitude values stored as strings ( VARCHAR
I have a table containing prices for a lot of different things in a
I have a table containing the runtimes for generators on different sites, and I
I have a table containing cells with text of various lengths. It is essential
I have a table containing 10 records. I want to fetch 5th and the

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.