Solved

Using a reference table to validate data on insert

Posted on 2015-02-19
4
53 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:
Scott Pletcher 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

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

Suggested Solutions

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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