all groups > sql server programming > july 2003 >
You're in the

sql server programming

group:

how to insert a new field (not the whole row) in SQL?


how to insert a new field (not the whole row) in SQL? John Davis
7/31/2003 6:51:39 PM
sql server programming:
given 2 tables T1 and T2, what is the SQL statement to add a new field for
each record in T2 when there are common records in both T1 and T2?

For example, T1 is:

ID Name
1001 N1
1002 N2
1003 N3

and T2 is

ID Name
1001 N1
1003 N3
1005 N5

After executing the SQL statement, then T2 should become

ID Name Common
1001 N1 Yes
1003 N3 Yes
1005 N5 No

Any ideas? Please advice! thanks!


John

Re: how to insert a new field (not the whole row) in SQL? Richard Ding
7/31/2003 10:27:42 PM
John,

Here is my 2 Cents:

select T2.ID,
T2.Name,
case when T1.ID is null then 'No'
else 'Yes'
end as 'Common'
from T2 left outer join T1
on T1.ID = T2.ID
AND T1.Name = T2.Name


Richard

[quoted text, click to view]

AddThis Social Bookmark Button