?
Solved

Using a reference table to validate data on insert

Posted on 2015-02-19
4
Medium Priority
?
57 Views
Last Modified: 2015-02-23
Hi,

Is it possible to validate a specific column on insert from one table to another using a reference table?.

Reference Table is as follows:

LineID INT    ProdCode VARCHAR (10)

Source table and target table both have column ProdCode VARCHAR (10)

If the ProdCode does not exist in the Reference table, then the insert to the target table should fail.

Any help would be appreciated.

Thanks
0
Comment
Question by:Morpheus7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40618962
You can use a FOREIGN KEY constraint. That will prevent any inserts from not being in the Reference table.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40618970
>If the ProdCode does not exist in the Reference table, then the insert to the target table should fail
A JOIN would do that, as it would limit the set to be inserted to only those ProdCode values that exist in both source_table and list_of_prod_codes.

INSERT INTO target_table (ProdCode, goo, foo, boo) 
SELECT s.ProdCode s.goo, s.foo, s.boo
FROM source_table s
   JOIN list_of_prod_codes pc ON s.ProdCode = pc.ProdCode

Open in new window


Then you'll have to ask yourself how to handle the rows where there is no matching ProdCode, such as do nothing, write another query and send the results to somebody, create a 'dummy' record in list_of_prod_codes, ...
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 40619122
Add a Foreign Key to prevent the insert:

ALTER TABLE dbo.target_table
    ADD CONSTRAINT target_table__FK_ProdCode
    FOREIGN KEY ( ProdCode ) REFERENCES dbo.reference_table ( ProdCode )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40619400
btw if you're using SSIS to perform this insert, then you can pull off the 'validation' with a data pump that has these components:
Data Source, whatever source_table is
Lookup:  Queries the list_of_prod_codes table, where you can join the two tables based on ProdCode.  This will result in two paths, Lookup Match and Lookup No match.
Data Destination:  Connect the 'Lookup Match' to your destination.
Then you can decide how you want to handle the 'Lookup No Match' path.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question