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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:15:49+00:00 2026-05-16T00:15:49+00:00

Writing my first SQL query to run specifically as a SQL Job and I’m

  • 0

Writing my first SQL query to run specifically as a SQL Job and I’m a little out of my depth. I have a table within a SQL Server 2005 Database which is populated each day with data from various buildings. To monitor the system better, I am attempting to write a SQL Job that will run a query (or stored procedure) to verify the following:

- At least one row of data appears each day per building

My question has two main parts;

  1. How can I verify that data exists for each building? While there is a “building” column, I’m not sure how to verify each one. I need the query/sp to fail unless all locations have reported it. Do I need to create a control table for the query/sp to compare against? (as the number of building reporting in can change)

  2. How do I make this query fail so that the SQL Job fails? Do I need to wrap it in some sort of error handling code?

Table:

Employee   RawDate                     Building 
Bob        2010-07-22 06:04:00.000     2
Sally      2010-07-22 01:00:00.000     9
Jane       2010-07-22 06:04:00.000     12
Alex       2010-07-22 05:54:00.000     EA
Vince      2010-07-22 07:59:00.000     30

Note that the building column has at least one non-numeric value. The range of buildings that report in changes over time, so I would prefer to avoid hard-coding of building values (a table that I can update would be fine).

Should I use a cursor or dynamic SQL to run a looping SELECT statement that checks for each building based on a control table listing each currently active building?

Any help would be appreciated.

Edit: spelling

  • 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-16T00:15:50+00:00Added an answer on May 16, 2026 at 12:15 am

    You could create a stored procedure that checks for missing entries. The procedure could call raiserror to make the job fail. For example:

    if OBJECT_ID('CheckBuildingEntries') is null
        exec ('create procedure CheckBuildingEntries as select 1')
    go
    alter procedure CheckBuildingEntries(
        @check_date datetime)
    as
    declare @missing_buildings int
    
    select  @missing_buildings = COUNT(*)
    from    Buildings as b
    left join
            YourTable as yt
    on      yt.Building = b.name
            and dateadd(dd,0, datediff(dd,0,yt.RawDate)) = 
                dateadd(dd,0, datediff(dd,0,@check_date))
    where   yt.Building is null
    
    if @missing_buildings > 0
        begin
        raiserror('OMG!', 16, 0)
        end
    go
    

    An example scheduled job running at 4AM to check yesterday’s entries:

    declare @yesterday datetime
    set @yesterday = dateadd(day, -1, GETDATE())
    exec CheckBuildingEntries @yesterday
    

    If an entry was missing, the job would fail. You could set it up to send you an email.

    Test tables:

    create table Buildings (id int identity, name varchar(50))
    create table YourTable (Employee varchar(50), RawDate datetime, 
                            Building varchar(50))
    
    insert into Buildings (name)
              select '2'
    union all select '9'
    union all select '12'
    union all select 'EA'
    union all select '30'
    
    insert into YourTable (Employee, RawDate, Building)
               select 'Bob', '2010-07-22 06:04:00.000', '2'
    union all select 'Sally', '2010-07-22 01:00:00.000', '9'
    union all select 'Jane', '2010-07-22 06:04:00.000', '12'
    union all select 'Alex', '2010-07-22 05:54:00.000', 'EA'
    union all select 'Vince', '2010-07-22 07:59:00.000', '30'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a little help writing a query. I have this data... vDir iNumber
I am writing my first little Access 2003 application. I have two queries that
I'm writing a query in SQL in which i have to find the youngest
(I'm a little new to SQL) I have a lot of queries I'm re-writing
I am writing my first dual app and cannot figure out how to give
I am writing my first FireFox extension and I have some questions. Maybe someone
I am trying to figure out how to go about writing a linq query
I'm writing a stored procedure in SQL Server 2008, interatively. When using SQL Server
Writing a SQL query that should return the average charges for a preceding 91
I am writing the result set of 1st database query result set to first

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.