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
 regarding update

Author  Topic 

mailtosaja
Starting Member

28 Posts

Posted - 2008-10-22 : 13:09:53
Hi,
see the below table.
ID|Name|EmailID
---------
1|Null|null
2|Null|null
3|Null|null
i would like to update Name and EmailID column at a Time(single query).Is it possible?

Thanks & Regards,
S.WoodSon.

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2008-10-22 : 13:15:59
yes
Go to Top of Page

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2008-10-22 : 13:16:16

Do you have a reference column for the where clause ?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-22 : 13:17:21
yup

UPDATE t
SET t.Name=t2.namevalue,
t.EmailID=t2.emailvalue
FROM table t
JOIN othertable t2
on t.linkingcol=t2.linkingcol

othertable is table from which you want to update. linking col are columns by which other table is related to your table.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2008-10-22 : 13:25:24
If you don't have othertable and want to type in the desired data:
update t1
set [Name] = t2.[Name], EmailID=t2.EmailID
from table1 t1 -- this your TargetTable
inner join
(select 1 as ID, 'Mr. x' as [Name], 70 as EMailID union
select 2, 'Mr. y', 71 union
select 3, 'Mr. z', 72) t2
on t1.ID = t2.ID

Webfred

No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

mailtosaja
Starting Member

28 Posts

Posted - 2008-10-22 : 13:25:35
Hi,
thanks for your reply.I have only one table..Is it possible please send me the query..
Thanks & Regards,
S.Woodson.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-22 : 13:28:27
quote:
Originally posted by mailtosaja

Hi,
thanks for your reply.I have only one table..Is it possible please send me the query..
Thanks & Regards,
S.Woodson.


then where will you get values from?
Go to Top of Page

mailtosaja
Starting Member

28 Posts

Posted - 2008-10-22 : 13:28:44
Hi,
Thanks for your reply.no. of Row changes that table...
Thanks & Regards,
S.WoodSon.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2008-10-22 : 13:35:36
quote:
no. of Row changes that table...
don't know what that means.
Was my solution not successful?
My Test works:
create table table1 (ID int, Name varchar(25), EMailID int)
insert table1 select 1,null,null
insert table1 select 2,null,null
insert table1 select 3,null,null
select * from table1
update t1
set [Name] = t2.[Name], EmailID=t2.EmailID
from table1 t1 -- this your TargetTable
inner join
(select 1 as ID, 'Mr. x' as [Name], 70 as EMailID union
select 2, 'Mr. y', 71 union
select 3, 'Mr. z', 72) t2
on t1.ID = t2.ID
select * from table1
drop table table1

You have to adjust the right IDs of entries without Name and MailId
Webfred

No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -