Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Append/Update columns from one table to another?

Posted on 2010-09-07
7
Medium Priority
?
282 Views
Last Modified: 2012-05-10
Hi there,

I have four columns of data in one table (frequency) within the same database that I wish to append/update into another table (company). I have tried an insert to statement but that didn't work due to the statement trying to create rows of which some of the fields cannot accept a null value.

I have revised my approach by using the attached code as means of trying to match the counter columns values to the same field in my company table. This code managed to loop through (incrementally) the company table and from this I would like to know if it would be possible for me to use possibly an IF statement to populate the company table with the fields from the frequency table when they are equal to each other, as both tables have a common realtion via the counter field?

Your help is much appreciated in advance
DECLARE @counter int
SET @counter = 0
UPDATE company
SET @counter = counter = @counter + 1

Open in new window

0
Comment
Question by:databarracks
  • 5
  • 2
7 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1500 total points
ID: 33615836
you want to update with a join?
http://www.experts-exchange.com/A_1517.html

not sure about your explanation(s) so far... a more concrete example might help ...
0
 

Author Comment

by:databarracks
ID: 33615993
Apologies for my rather confusing query. Yes I am trying to update existing rows and need the update to begin from the beginning of the row and work its way down. The below code from the link seems useful but I am not sure about the 'WHERE' clause? How would I write it to say that the 'counter' field in the frequency table equals the 'counter' field in the company table?


0
 

Author Comment

by:databarracks
ID: 33616050
To be honest all I am aiming to do is update the existing fields and rows in my company table with the values in the frequency table for the fields that are pervailent in both tables which are:

(contact_method, contact_frequency, contact_other, auto_email)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33616059
can you please show sample data before and after the update?
0
 

Author Comment

by:databarracks
ID: 33616118
Hi,

Please find attached sample data. Kindly note that the (contact_method, contact_frequency, contact_other, auto_email) data values should be copied/appended/updated from the frequency table.
Sample-Data.xlsx
0
 

Author Comment

by:databarracks
ID: 33616195
I have found the solution from the link provided, thank you.
0
 

Author Closing Comment

by:databarracks
ID: 33616200
Helpful
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question