SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Add column into an existing table and add values


Add column into an existing table and add values

Author
Message
NewBornDBA2017
NewBornDBA2017
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3818 Visits: 924
I just created a backup table from an existing table (The existing table contain some values in 'Marital Status' column like single or married etc.). I just dropped the 'Marital status' column from the backup table and now I am adding back that column into the backup table again (Marital Status)
alter table DatFileBak add MaritalStatus varchar(10) 
What I am trying to learn here is I want to be able to add the column into a table and add data in a way that it matches customer key. I am not sure if this question is confusing but let me know if it is.

Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)

Group: General Forum Members
Points: 214017 Visits: 24687
add data in a way that it matches customer key


Please expand on the above.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Alejandro Santana
Alejandro Santana
SSC Eights!
SSC Eights! (871 reputation)SSC Eights! (871 reputation)SSC Eights! (871 reputation)SSC Eights! (871 reputation)SSC Eights! (871 reputation)SSC Eights! (871 reputation)SSC Eights! (871 reputation)SSC Eights! (871 reputation)

Group: General Forum Members
Points: 871 Visits: 1072
Phil Parkin - Tuesday, February 6, 2018 11:24 AM
add data in a way that it matches customer key


Please expand on the above.

What i believe is that he already has another table with the column and that data and the idea is to insert into the new table and column the marital status of the other table containing the data of course, matching the ID on both tables, please correct me if im wrong.

NewBornDBA2017
NewBornDBA2017
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3818 Visits: 924
Phil Parkin - Tuesday, February 6, 2018 11:24 AM
add data in a way that it matches customer key


Please expand on the above.

Giving you the real scenario. I have table which contains customer information such as name, address, email address etc. What I need to do is I need to add 2 more columns (education and race). We pull the data the website and now I need to make sure that the education and race data is getting stored properly. For example There is a customer Name John Travolta and his education is Master in CIS and he is white. How do I store those values for let's say 1.5 customers? So when we pull the data for a customer, we have all the information. Does it make sense?

Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)

Group: General Forum Members
Points: 214017 Visits: 24687
NewBornDBA2017 - Tuesday, February 6, 2018 12:37 PM
Phil Parkin - Tuesday, February 6, 2018 11:24 AM
add data in a way that it matches customer key


Please expand on the above.

Giving you the real scenario. I have table which contains customer information such as name, address, email address etc. What I need to do is I need to add 2 more columns (education and race). We pull the data the website and now I need to make sure that the education and race data is getting stored properly. For example There is a customer Name John Travolta and his education is Master in CIS and he is white. How do I store those values for let's say 1.5 customers? So when we pull the data for a customer, we have all the information. Does it make sense?

Sure it makes sense. But which bit do you require assistance with?
1) How to add columns to tables?
2) How to populate columns from data held in other tables?
3) How to transfer data from a web form to a SQL Server database table?



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
NewBornDBA2017
NewBornDBA2017
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3818 Visits: 924
Phil Parkin - Tuesday, February 6, 2018 12:43 PM
NewBornDBA2017 - Tuesday, February 6, 2018 12:37 PM
Phil Parkin - Tuesday, February 6, 2018 11:24 AM
add data in a way that it matches customer key


Please expand on the above.

Giving you the real scenario. I have table which contains customer information such as name, address, email address etc. What I need to do is I need to add 2 more columns (education and race). We pull the data the website and now I need to make sure that the education and race data is getting stored properly. For example There is a customer Name John Travolta and his education is Master in CIS and he is white. How do I store those values for let's say 1.5 customers? So when we pull the data for a customer, we have all the information. Does it make sense?

Sure it makes sense. But which bit do you require assistance with?
1) How to add columns to tables?
2) How to populate columns from data held in other tables?
3) How to transfer data from a web form to a SQL Server database table?

So the 1st part is done, but I need help with the second part. How to populate the race and education data based on the customer key?
Or is it better to have a separate column for race and education but I am not sure if that's going to make any sense. It will only make sense if someone has multiple race or even multiple education. But if are only concerned about 1 record of education (highest degree and not bachelor's or master's or PhD etc.), then adding 2 columns into an existing table should work.

Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)

Group: General Forum Members
Points: 214017 Visits: 24687
NewBornDBA2017 - Tuesday, February 6, 2018 1:09 PM
Phil Parkin - Tuesday, February 6, 2018 12:43 PM
NewBornDBA2017 - Tuesday, February 6, 2018 12:37 PM
Phil Parkin - Tuesday, February 6, 2018 11:24 AM
add data in a way that it matches customer key


Please expand on the above.

Giving you the real scenario. I have table which contains customer information such as name, address, email address etc. What I need to do is I need to add 2 more columns (education and race). We pull the data the website and now I need to make sure that the education and race data is getting stored properly. For example There is a customer Name John Travolta and his education is Master in CIS and he is white. How do I store those values for let's say 1.5 customers? So when we pull the data for a customer, we have all the information. Does it make sense?

Sure it makes sense. But which bit do you require assistance with?
1) How to add columns to tables?
2) How to populate columns from data held in other tables?
3) How to transfer data from a web form to a SQL Server database table?

So the 1st part is done, but I need help with the second part. How to populate the race and education data based on the customer key?
Or is it better to have a separate column for race and education but I am not sure if that's going to make any sense. It will only make sense if someone has multiple race or even multiple education. But if are only concerned about 1 record of education (highest degree and not bachelor's or master's or PhD etc.), then adding 2 columns into an existing table should work.

Regarding adding multiple columns for race and education, that's a design choice for you to make. But if you find yourself creating more than a couple of either, you're rapidly heading into a bad place & should seriously consider normalising your design.
Regarding the query: without a full description of table or column names, the general structure is as follows:
UPDATE target
SET MaritalStatus = source.MaritalStatus
FROM DatFileBak target
JOIN Tab2 source on target.CustomerKey = source.CustomerKey

The above query assumes a 1-1 link between the source and target tables on the match key ... this is very important.



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search