Solved

corresponding IDs in different tables

Posted on 2013-12-16
8
159 Views
Last Modified: 2013-12-16
I have Table1 and Table2

tblID1 is a string in Table1 and tblID2 in Table2 is a string nchar10

tblID1 in Table1 needs to correspond with all the tblID2 values in Table2.  Is there a way to link the values in Table1 to Table2 if they change in Table1?

For example, if I create a record in Table1 with tblID1 = 1, then tblID2 = 1.  However if I change ID1 = 5, then tblID2 = 5

Is there a way to do that?
0
Comment
Question by:al4629740
[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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 5

Expert Comment

by:dannygonzalez09
ID: 39721876
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39721883
>Is there a way to link the values in Table1 to Table2 if they change in Table1?
Just a thought ... if Table2.tblID2 should always be the same value as Table1.tblID2, then it would make sense to not have column Table2.tblID2, and in queries JOIN the two tables with the below snippet in the SELECT clause
SELECT Table1.tblID1 as TblID2
FROM Table1
   JOIN Table2 ON Table1.Whatever = Table2.Whatever

Open in new window

0
 

Author Comment

by:al4629740
ID: 39722048
Jim, the "whatever" would be the tblID1 and tblID2

Something needs to identify the connection between the records of two separate tables.
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:al4629740
ID: 39722052
How do I create a foreign key in the design mode of the table?
0
 

Author Comment

by:al4629740
ID: 39722059
I am using 2012 sql server
0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 250 total points
ID: 39722103
>Jim, the "whatever" would be the tblID1 and tblID2
Uhh, no, not if tblID2 keeps changing as tblID1 changes.

One solution is a foreign key relationship (mentioned above) with a cascade update, so that tblID2 changes as tblID1 changes for a given tblID1 value.

Or ... manually run an UPDATE statement every time tblID1 changes, with pouplated @the_new_value and @the_old_value variables, like this..

UPDATE Table2
SET tblID2 = @the_new_value
WHERE tblID1 = @the_old_value
0
 
LVL 32

Expert Comment

by:awking00
ID: 39722179
You might create an after update trigger on table1.tblid1 to update table2.
0
 
LVL 5

Accepted Solution

by:
dannygonzalez09 earned 250 total points
ID: 39722188
steps to create a foreign key

http://technet.microsoft.com/en-us/library/ms189049(v=sql.105).aspx

As Jim suggests, foreign key with an update should keep the Id's in sync
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CAST issue with SQL 2000 15 44
SQL Server 2012 to SQL Server 2016 24 57
Need more granular date groupings 4 45
Enable TLS 1.2 for SQL 2012 Web Edition 1 25
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

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