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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:12:50+00:00 2026-05-13T14:12:50+00:00

I am saddled with an ERP database which lacks any foreign keys and therefore

  • 0

I am saddled with an ERP database which lacks any foreign keys and therefore lacks referential integrity.

I am writing a script to check most of the major tables in our database for ophaned records.

For example, in this case I’m working with our Sales tables.

SLCDPM - Customer Master Table
SOMAST - Sales Order Master
SOITEM - Sales Order Items
SORELS - Sales Order Releases

Basically, for these (and a whole bunch of other tables) I need to check to see if there are records in the SORELS that don’t appear in any table above it. Then take SOITEM and check above it. Etc.

I started writing scripts, but the number of lines gets kind of ridiculous. Here is where I started with just these 4 tables.

select 'Sales Order Master',* from somast where fcustno not in (select fcustno from slcdpm where ftype <> 'P')
SELECT 'Sales Order Item',* FROM soitem WHERE fsono NOT IN (SELECT fsono FROM somast)
select 'Sales Order Release',* from sorels where (fsono+finumber) not in (select (fsono+finumber) from SOITEM)

The reason I stopped was that I just realized that SORELS (the bottom table) only checks the table before it, not all of the tables before it.

Anyone know of a script I can use to make this more automated or a better way to do it?

  • 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-13T14:12:51+00:00Added an answer on May 13, 2026 at 2:12 pm

    And people sell database junk like this; it always amazes me what I see in commercial products.

    This is genuine case for dynamic sql and a cursor, I think. This is exactly the kind of one-time administrative function that is reason why these techniques exist (they weren’t really meant for production code mostly, but for administrative tasks).

    I’d create a table showing each table and the table I think it should have a foriegn key to. (You may even be able to populate this from the system tables if they at least had a good naming convention).

    Then I would use a cursor to go through the table and create the sql dymanically for looking to see if the FK table has orphaned records.

    It’s still alot of code but at least you didn’t have to write it all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been saddled with supporting an old Access 2003 database (with SQL backend) produced
So I've been saddled with furthering a website, in which it is crucial that
I have got myself saddled with an existing application - which attempts do do
I have been saddled with using an in-house data access library that is effectively
I'm currently working with a fairly old product that's been saddled with a lot
The lackluster response here made me wonder this . I've been saddled with a
Let's say I've got a vector like this one: A = [101:105] Which is
Our product is a distributed system. The modules I work on are fairly new,
Having dealt with yet another stupid eclipse problem, I want to try to get
Ok saddle up cowboys, because this is going to be a long one. I

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.