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
 General SQL Server Forums
 New to SQL Server Programming
 matching columns

Author  Topic 

jfm
Posting Yak Master

145 Posts

Posted - 2013-04-22 : 10:44:23
Hi there,

I have table1 with col_date and col_id

I need to copy col_date from table1 into table_2, when Col_id = Col_id from table2

Any idea?

thank you

jfm
Posting Yak Master

145 Posts

Posted - 2013-04-22 : 10:48:26

Hi there,

I have table1 with col_date and col_id

I need to copy col_date from table1 into table_2, when Col_id from table1 = Col_id from table2

Any idea?

thank you
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2013-04-22 : 11:14:24
Use the UPDATE and JOIN , here is an example , using the details you posted :
create table #temp1
(col_id INT , col_date datetime)

create table #temp2
(col_id INT , col_date datetime)

INSERT INTO #temp1
select 1, '01-01-2001'
UNION
select 2,'02-02-2001'

INSERT INTO #temp2
select 1, '03-03-2001'
UNION
select 2,'04-04-2001'


UPDATE #temp2 SET col_date = B.col_date
FROM #temp2 AS A
INNER JOIN #temp1 AS B
ON A.col_id = B.col_id




drop table #temp1
drop table #temp2



Read more on UPDATE and JOIN on http://www.sqlserver-dba.com/2011/06/t-sql-update-join-on-a-table.html

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2013-04-22 : 11:31:56
Select Col_Id, col_date from Table1
inner join table
on table1.col_id = table2.col_id

Im missing something here to make it work

Any tip?

Thanks
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2013-04-22 : 15:09:43
That will return the combined recordset from Table 1 and Table 2 - of rows joined at col_id.
Your initial request is to UPDATE ?

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page

jfm
Posting Yak Master

145 Posts

Posted - 2013-04-23 : 04:18:30
I want to gather the info that connects via id_Col.

But is not working properly...

No Jackv, Im looking to extract the data, not update. but still not working the query..

Thank you
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-04-23 : 04:30:01
Follow these links to post your query

http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
http://www.sqlservercentral.com/articles/Best+Practices/61537/



--
Chandu
Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2013-04-23 : 09:50:23
You say it's not working properly - as suggested by bandi - could you supply , the DDL - an expected resultset and the current resultset

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page
   

- Advertisement -