Solved

SQL Append/Update columns from one table to another?

Posted on 2010-09-07
7
264 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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 142

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now