Update only one column in SQL table

Shweta

Well-known Member
Joined
Jun 5, 2011
Messages
514
Hi All,

I have a table with approx 200 records for Advisor details. I have added a new column 'Email_Id'. EmailId is First_Name.Last_name@domain.com. Please help with a query to loop through all records to update this column.

First_Name and Last_Name is already their in the table.

Thanks in advance!

Regards,
Shweta Jain
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
if you left it with the names, then you can add the domain in your sql script i.e first + ',' + last + '@domain.com' as you query
 
Upvote 0
Thanks mole999. But I don't know how to do it for 200 records at once. Please help with a complete script.
 
Upvote 0
he's saying that instead of adding the new column you could just change your queries

every time you need the email from the table just write
select tbl.First_Name & "." & Last_Name & "@domain.com" from tbl

that way you build email every time you need it
this has a big advantage
let's say one of your employees gets married and changes their last name

your way you'd have to change the Last_Name column and you'd also have to change the new
Email_Id column

his way you'd only have to change the Last_Name column

because the email address would be automatically built using the corrected Last_Name every time you query the able


 
Upvote 0
Thanks for this. I know this alternative. However my query was something different.

I just want to clear the concept of looping through all the records in an existing SQL table. In case I have to add a new column in an existing table then what's the solution. Do I need to write update statement 200 times or there is any other option using loop or something else. So that I can start a loop from 1 to 200 and write update statement within this loop. Please suggest.
 
Last edited:
Upvote 0
a simple update statement should do it




first try
Rich (BB code):
select 
 tbl.First_Name & '.' & Last_Name & '@domain.com' as [email_id]
from 
  tbl


if the results look correct then run


Rich (BB code):
update  
  tbl 
set 
 Email_Id = tbl.First_Name & '.' & Last_Name & '@domain.com'


running the update one time will fill the column for every row


Microsoft Access Update Query Examples, SQL Syntax, and Errors


SQL: UPDATE Statement
 
Upvote 0
One more query..In this case First Name and Last Name was available in the table. So it was quite easy to update the data in the newly added column by giving the reference of existing columns and adding domain name at the end.

What if I need to update bank account numbers or some other detail which is unique for each advisor in the table?
 
Upvote 0

Forum statistics

Threads
1,221,813
Messages
6,162,117
Members
451,743
Latest member
matt3388

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top