SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 matching columns
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jfm
Posting Yak Master

145 Posts

Posted - 04/22/2013 :  10:44:23  Show Profile  Reply with Quote
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 - 04/22/2013 :  10:48:26  Show Profile  Reply with Quote

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
Flowing Fount of Yak Knowledge

United Kingdom
2067 Posts

Posted - 04/22/2013 :  11:14:24  Show Profile  Visit jackv's Homepage  Reply with Quote
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 - 04/22/2013 :  11:31:56  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

United Kingdom
2067 Posts

Posted - 04/22/2013 :  15:09:43  Show Profile  Visit jackv's Homepage  Reply with Quote
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 - 04/23/2013 :  04:18:30  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 04/23/2013 :  04:30:01  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

United Kingdom
2067 Posts

Posted - 04/23/2013 :  09:50:23  Show Profile  Visit jackv's Homepage  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000