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
 Insert to table from another

Author  Topic 

temuri
Starting Member

8 Posts

Posted - 2011-03-26 : 21:23:03
HI! I have two tables. with almost same data

Tabl1
ID,userID,username,description


Tabl2
ID,userID,username,description

I want to transfer description from Table1 to Table2 where userID are same and not exists

please help!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-03-26 : 23:09:44
UPDATE t2
SET description = t1.description
FROM Table2 t2
JOIN Table1 t1
ON t2.userID = t1.userID
WHERE t2.description IS NULL

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-03-27 : 01:09:51
you want to insert the new description also as a new value or overwrite the existing one?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

temuri
Starting Member

8 Posts

Posted - 2011-03-27 : 06:24:50
I want to add description fromt table1 to table2 where description is null
Go to Top of Page

temuri
Starting Member

8 Posts

Posted - 2011-03-27 : 06:25:19
tkizer I will try.. tank you
Go to Top of Page

temuri
Starting Member

8 Posts

Posted - 2011-03-27 : 06:43:57
and also where date value where I want to compare with date(month, year) only values where month and year in date value is same.

thank you
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-27 : 07:21:23
In the list of columns in your original posting, you didn't show a date column. But, if you do, you can add the new conditions to Tara's query as in
quote:
Originally posted by tkizer

UPDATE t2
SET description = t1.description
FROM Table2 t2
JOIN Table1 t1
ON t2.userID = t1.userID
WHERE t2.description IS NULL
and year(t1.dateColumn) = year(t2.dateColumn)
and month(t1.dateColumn) = month(t2.dateColumn)


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

Go to Top of Page
   

- Advertisement -