Solved

Using a reference table to validate data on insert

Posted on 2015-02-19
4
50 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
  • 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 65

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:
ScottPletcher earned 500 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 65

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now