Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to populate a foreign key..

Author  Topic 

giviglie
Starting Member

5 Posts

Posted - 2009-01-07 : 07:43:08
Hi...
I will try to explain my problem.
I have 2 to tables.
In the first table I have 2 colums: ID (int primary key), MyDate (DateTime).
In the second Table for now I save in a DateTime column the MyDate value from the first table.
Now I was asked to save not the date but the ID from the first table in a new column in Table 2 called IDMyDate.
All the tables already contain records.

My boss asked me to create a script that populates the Table2.IDMyDate Column with the right ID from Table1. So I had never done a stuff like this and I am in a really big trouble.

Any suggestion will be very appreciated!

Thanx

Giacomo

raky
Aged Yak Warrior

767 Posts

Posted - 2009-01-07 : 07:47:49
Update fk
set fk.idmydate = pk.id
from primarykeytable pk
inner join foreignkeytable fk on fk.commoncolumn = pk.commoncolumn

or just

update foreignkeytable
set idmydate = id

what is the need of two columns containing same values
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-07 : 10:08:02
[code]Update t2
set t2.idmydate = t1.id
from table1 t1
inner join table2 t2
on t2.DateColumn= t1.MyDate
[/code]
Go to Top of Page
   

- Advertisement -