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 644595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:24:37+00:00 2026-05-13T21:24:37+00:00

I have a table that functions as an event log and stores the users

  • 0

I have a table that functions as an event log and stores the users signed in state, ‘In’, ‘Out’, or ‘Rejected’ (sometimes users my be ‘Rejected’ based on external criteria).

Here is some sample data so you can get an idea of what the table looks like:

Table MyTable
PersonID - State       - DateTime
// data sample
156      - 'Out'       - 02-14-2010 13:04:15
156      - 'In'        - 02-21-2010 09:01:13
16       - 'In'        - 02-21-2010 09:05:01
58       - 'Rejected'  - 02-21-2010 11:04:58
156      - 'Out'       - 02-21-2010 11:10:02

Here is some pseduo check restraint code outlining what I’d like to do:

CHECK(
        CASE
            WHEN (
                [State] = 'In' AND
                (Select TOP 1 State FROM MyTable WHERE PersonID=@PersonID_ToUpdate)!='In' ORDER BY DateTime DESC)
            )
            THEN 'T'
            WHEN (
                [State] = 'Out' AND
                (Select TOP 1 State FROM MyTable WHERE PersonID=@PersonID_ToUpdate)!='Out' ORDER BY DateTime DESC)
            )
            THEN 'T'
            WHEN (
                [State] = 'Rejected' AND
                (Select TOP 1 State FROM MyTable WHERE PersonID=@PersonID_ToUpdate)!='In' ORDER BY DateTime DESC)
            )
            THEN 'T'
            ELSE 'F'
        END = 'T'
)

Basically:

  • A person can sign IN if their last state was not ‘In’
  • A person can sign OUT if their last state was not ‘Out’
  • A person can be REJECTED if their last state was not ‘In’

I don’t know if a Check Constraint is the best way to do this or if my database design will allow for this level of constraint; please let me know if I’m out of my mind (and kindly suggest a more suitable method for storing the data and/or ensuring data integrity)

note: I’m using SQL-Server 2008

  • 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-13T21:24:38+00:00Added an answer on May 13, 2026 at 9:24 pm

    Here’s a sample trigger. It assumes that you’re only going to insert 1 row at a time (which is probably the case here), and I haven’t bothered with indexes, etc.

    I added a clause for when the state is ‘Out’ so it ignores ‘Rejected’ states – this was to prevent multiple Out’s. Its very basic but you get the idea.

    if object_id('dbo.MyTable') is not null     
        drop table dbo.MyTable;
    
    create table dbo.MyTable (
        PersonID int not null,
        [State] varchar(20) not null,
        [DateTime] datetime not null default(getdate())
        ); 
    
    if object_id('dbo.ins_MyTable_status_validation') is not null drop trigger dbo.ins_MyTable_status_validation;
    go
    create trigger dbo.ins_MyTable_status_validation
        on dbo.MyTable
        instead of insert
    as 
    begin
        set nocount on;
    
        -- assuming you're only inserting 1 row at a time (which makes sense for an event log)
        if (select count(*) from inserted) > 1 begin
            print 'Multiple rows inserted - raise some kind of error and die'
            return
        end
    
        declare @personid_toupdate int,
                @state varchar(20);
    
        select  @personid_toupdate = personid,
                @state = [state]
        from    inserted;
    
        if case
            when (
                @state = 'In' and
                isnull((select top 1 [State] from dbo.MyTable where personid = @personid_toupdate order by [datetime] desc), 'Blah') != 'In'
                )
                then 'T'
            when (
                @state = 'Out' and
                isnull((select top 1 [State] from dbo.MyTable where personid = @personid_toupdate and [State] != 'Rejected' order by [datetime] desc), 'Blah') != 'Out' 
                )
                then 'T'
            when (
                @state = 'Rejected' and
                isnull((select top 1 [State] from dbo.MyTable where personid = @personid_toupdate order by [datetime] desc), 'Blah') != 'In'
                )
                then 'T'
                else 'F'
            end = 'T'
        begin
            -- data is valid, perform the insert
            insert  dbo.MyTable (PersonID, [State]) 
            select  PersonID, [State]
            from    inserted; 
        end
        else
        begin
            -- data is invalid, return an error (something a little more informative than this perhaps)
            raiserror('bad data...', 16, 1)
        end
    end
    go
    
    -- test various combinations to verify constraints
    insert  dbo.MyTable (PersonID, [State]) values (1, 'In')
    insert  dbo.MyTable (PersonID, [State]) values (1, 'Out')
    insert  dbo.MyTable (PersonID, [State]) values (1, 'Rejected')
    
    select * from dbo.MyTable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.